Skip to Main Content
Cognex 지원 허브
LogoLogo

How to Sort the Order of Multicode Reading Results According to The Contents

How to Sort the Order of Multicode Reading Results According to The Contents

2025. 05. 12.

Question

How to sort the order of multicode reading results according to the contents.

Answer

You can sort and change the output order of Multicode reading results, prepared Sorting Priority in the setting GUI is useful. It supports sorting by symbol type, image order and position. But cannnot cover soring by contents. If you want to sort the multiple reading results according to thoes contents, you can use following script.

Reset Configuration of the device and enable Script-Based Formatting, then copy and paste the below code into the Data Formatting Script, overwriting any existing code there.

Placeholder
placeholder

 

function onResult (decodeResults, readerProperties, output)
{
    if (decodeResults[0].decoded)
    {
        output.content ="";
        
        var resultArray = [];
        for(var i=0; i<decodeResults.length; i++)
            resultArray.push(decodeResults[i].content + "\r\n");

//Following should be described sorting logic.
//This sample is using the sort() method of the Array object.
//This method sorts an array of strings in ascending order according to their contents.
//e.g.1:  if the array is ["Da", "ta", "Ma", "n"], the sorted result will be ["Da", "Ma", "n", "ta"].
//e.g.2:  if the array is ["110", "300", "100", "20"], the sorted result wil  be ["100", "110", "20", "300"].
//If you want more strict sorting logic, you can replace this line to your own program.
resultArray.sort();
        
        for(var j=0; j<resultArray.length; j++)
            output.content += resultArray[j];
    }
}

관련 자원

Loading component...