3
I have a List<List<string>>
and need to sort the main list according to some elements of the secondary see the illustration below.
I need the first level to be in alphabetical order, and the second level to be in ascending order but according to the numbers. For example the number 26 has to come before 1000.
The code below does something similar but on the second level it orders according to the string
, then the 1000 comes before the 26.
var MySortedList = MyUnsortedList.OrderBy( x => x[1] ).ThenBy( x => x[2] );
I would have to convert it to int and do the sort in a few strings I have /
and -
. Then you’d have a Exception
. I have no idea how to implement it.
And how should this data be treated? They are not numbers, they contain numerical digits, which is quite different. Without a criterion of what you must do, any solution is wrong, and the correct criterion we do not know, only you know.
– Maniero
All data are strings. However, this data is presented in a Listview. To make it easier for the user to find the element I need to sort them. Then in the first level appears in alphabetical order. A, B, C ... Accessing "A". I need it to show the numbers in ascending order. 1,2,3,4.... The numbers with / and - we can only consider the first part of the string with a split('/') or split('-'). I don’t know if I was too clear.
– MRS
And why didn’t you do that
split()
?– Maniero
So I can split, but I don’t know how to implement split('/'), split('-'). Convert to int and implement in the above code. So he can sort the numbers in ascending order.
– MRS
I posted an image of my listview. See that in the second level is not in numerical order.
– MRS
Ideally it would be better if you had a code so we could test it. You can ensure that all elements start with valid numbers?
– Maniero
At the moment a few start with A, AM, C. But if there is no other way I can delete this data from the list.
– MRS
You need to decide what to do with these cases. The whole problem isn’t the code, it’s the lack of criteria for how to solve these cases.
– Maniero
See in the listview image. I need the first part of the numbers to appear in numerical and not string order like this. And I need to implement this in the ordering of the code I posted as an example.
– MRS
And what do you do with these cases that are letters?
– Maniero
They stay at the bottom of the list as it is happening now.
– MRS
Alphabetically?
– Maniero
Yes. That’s right.
– MRS
What is the maximum number of digits you can have before the first bar? What is after the bar doesn’t matter?
– Maniero
The amount before the slash is undetermined. I need to order only what is before the slash. What is after either does the order.
– MRS
Don’t have a maximum? It can have 1 million digits?
– Maniero