LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

sorting string by first character


@Yamaeda wrote:

Dont sort 1D array of strings sort by 1st character already? If not, then extract 1st with String subset and bundle it with the full string in a loop (the single character as 1st element), and sort the cluster array. If you need the string array back, unbundle in a 2nd loop.

/Y


This will still do a secondary sort by the remaining characters.

Message 11 of 16
(893 Views)

@altenbach wrote:

 

This will still do a secondary sort by the remaining characters.


I noticed that when I tried it early in this thread's life; I certainly didn't expect that sorting behavior.  It happens regardless of which element of the cluster comes first.  Can you 'splain?

 

Example_VI.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 12 of 16
(884 Views)

@jcarmody wrote:

@altenbach wrote:

 

This will still do a secondary sort by the remaining characters.


I noticed that when I tried it early in this thread's life; I certainly didn't expect that sorting behavior.  It happens regardless of which element of the cluster comes first.  Can you 'splain?


Because the first element is just the first priority for the sorting.  It sorts by the first element and for anything that is the same, it will sort by the second element.  So with this exact example, you are basically always sorting on the full array.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 13 of 16
(881 Views)

Your sort by the first letter element (since it is first), there are some matches, so it sorts by the full name element.

 

Or your sort by the full name element (since it is first), the first letter element is going along for the ride.  In the end, same result.

 

I didn't realize sorting on clusters also did a secondary sort on the second element.

 

A way around this.  Build a cluster consisting of  the first letter, a sequential number based on the location in the original array, then the actual word itself.

It will sort on the first letter, then sort on the sequenctial number.  Now elements that have the same first letter will maintain the same order they had in the original array.  The third element of the actual name will come along for the ride since there is no need to do a sort on the third element.

 

I still wonder why there is a need to only sort on the first element and not care about sorting on the word.  Why not just sort on the word and be done with it?  What is so special about keep the word in the same relative order if they have the same first letter?

Message 14 of 16
(879 Views)

Now I understand; thanks.

 

Example_VI.png

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 15 of 16
(867 Views)

Here's what I would do. (Seems a bit simpler and faster because it avoids the linear search)

 

 

Message 16 of 16
(857 Views)