07-19-2013 11:58 AM
@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.
07-19-2013 01:01 PM
@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?
07-19-2013 01:07 PM
@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.
07-19-2013 01:10 PM
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?
07-19-2013 01:28 PM
07-19-2013 01:45 PM - edited 07-19-2013 01:47 PM
Here's what I would do. (Seems a bit simpler and faster because it avoids the linear search)