Case-insensitive string sorting
Today I'm gonna talk about a very simple issue: sorting a string array. In LabVIEW, the default behavior is a case-sensitive sorting. That's to say, "Beta" will be sorted above "alfa", just because upper case letters are considered to be before their lower case counterparts.
Obviously, sometimes we'll need a case-insensitive sorting, while not losing the original casing (let's say, when sorting a filenames list). So, what's the fastest and simplest way of achieving it? I'm partial to this one: building a cluster array where the first element is the lower case version of our string, and the second one is the original string.
When sorting clusters, LV will do it based on the first element, so after that we just need to extract the second element from each cluster in the array, and it's done!
Download (VI for LabVIEW 2016)
Incidentally, this VI exposes the Sort 1D Array primitive, that has been hidden on the last LabVIEW versions in favor of a malleable VI (nothing bad about it, but I'm sure the primitive is faster).
Non-ASCII characters
So far, so good. Unfortunately, if our strings potentially include non-ASCII characters, like a lot of human languages do, the sorting algorythm will be wrong once again, since, for example, "Á" will be put after "Z". Here we'll need a bit of cranking, using the same concept but replacing the special letters by their counterparts.
In the case of Spanish, the conversion is:
Special char | Code | To code | Char |
---|---|---|---|
á | 225 | 97 | a |
é | 233 | 101 | e |
í | 237 | 105 | i |
ó | 243 | 111 | o |
ú | 250 | 117 | u |
ñ | 241 | 110 | n |
Here you have the VI. Obviously, you can add new cases reflecting any addtional special character you want to sort correctly.
Download (VI for LabVIEW 2016)
Hope you'll find it useful.
Comentarios
Publicar un comentario