Question sorting lists

andrews

Well-known member
Joined
Nov 22, 2011
Messages
132
Programming Experience
5-10
I have a list of strings.
These strings are f.e. 5,8...a,k...
When I sort then 5,8... is placed before a, k...
Can I sort the list so that I get a,k,...,5,8..
Thanks for any response.
 
Providing a single example of a result is useless. Please describe the actual rules you want to apply to perform the sort because it's those rules that any code will need to implement. Writing code without knowing what the code actually has to do is always a challenge. The result is not what the code has to do. The steps to get to that result is what the code has to do so if you haven't considered the steps then you shouldn't even be thinking about writing code.
 
I do not think that an example is useless.
Let's take another
cd,bac,31,bca,af,11,23,08
Sorting this
08,11,23,31,af,bac,bca,cd
But I want
af,bac,bca,cd,08,11,23,31
I think that the problem is clear
I see one a solution, I have to do something before because vb.net can't do it immediately
The set of elements are from another greater and known set of 100 elements
so I can with a select case gaves all the elements an known prefix 100 or 200
125cd,115bac,20031,120bca,110af,20011,20023,20008
When I sort now I get a good ranking and I must now remove the prefix
Sorry if I was not clear in my problem.
Thanks for your answer, see you maybe a better way?
 
There are always multiple ways that a specific list can be sorted to give a specific result. Many of those ways will not produce the desired result for many other lists. I don't want to guess because I don't want to guess wrong. If it's too much effort for you to describe the actual requirements then it's too much trouble for me to provide a solution that may or may not be a waste of time.
 
If you just want standard string comparison and then move the group of strings that starts with a digit to the end then that can easily be done with Linq:
Dim reorder = (From x In strings
               Order By x
               Group By bool = Char.IsDigit(x(0))
               Into g = Group Order By bool
               ).SelectMany(Function(y) y.g)

This can of course also be done with a standard comparison function where you check if first char is a digit and conditionally compare x to y or reverse y to x.

I also agree with jmcilhinney that you haven't explained the required sorting well.
 
Back
Top