LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
MichaelBalzer

Use numeric sorting rather than literal sorting

Status: New

It'd be great if when sorting a column of items (say in the Multiple Variable Editor, or a table of data), the values were sorted using a numeric sort rather than a literal sort (ie. DI10 should not come after DI1, and DI20 should not come after DI2).

 

channel_sort_wrong_annotated.png

 

There could also be a specialised string sort function which takes in a string array and provides an option for a numeric or literal sort.

 

 




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
4 Comments
ToeCutter
Active Participant

Thing is, although it's obvious in this instance what order you want, does CAT come before or after CA1? I realise I'm stating the obvious here and it probably doesn't help you out, but if you're in control of the naming, making your numeric fields fixed width with leading zeros gets the right order.

MichaelBalzer
Active Participant

Leading zeroes are an option in some cases, and one which I've used where it makes sense (tag names, file names, etc). In the included example, I don't have control over the naming as they are hardware channel names.

 

Microsoft put out a KB article about numeric sorting in Windows, and provides a few examples. I'd imagine the numeric sorting in LabVIEW would be identical. So the example of CA1 and CAT would be sorted in that order, where numbers come before text.

 

I think the channel list example I provided is obvious, because numeric sorting is obvious! Smiley Happy




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
DerekTrepanier
Member
the "easy" way to sort things is by ASCII order, which leads to the order you're seeing. This is how computers like to order things. The "right" way to sort is using a more complicated sorting algorithm called a "Natural Sort". This sorts them the way a human likes to order things. https://en.wikipedia.org/wiki/Natural_sort_order I think all UI elements should be sorted using a natural sort algorithm.
RnDMonkey
Active Participant

I had exactly this consternation in one of my projects. Since COM ports don't have leading zeros, my fix was to use a Regex search to split the number off the end of the array elements, convert them to an array of integers, then sort the array of these and use the new indexes to reorder the original string array. Since the OpenG sort array has sorted indexes as an output, I just wired that right into the "Reorder Array" OpenG function operating on the original array of strings. That was my implementation of a "Natural Sort" but it requires knowing where the important numbers are (or else implementing a true natural sort order function).

____
Ryan R.
R&D