How to sort a string list?

Asked

Viewed 1,959 times

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.

listas

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.

Tela da aplicação

  • 1

    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.

  • 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.

  • And why didn’t you do that split()?

  • 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.

  • I posted an image of my listview. See that in the second level is not in numerical order.

  • 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?

  • 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.

  • 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.

  • 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.

  • And what do you do with these cases that are letters?

  • They stay at the bottom of the list as it is happening now.

  • Alphabetically?

  • Yes. That’s right.

  • What is the maximum number of digits you can have before the first bar? What is after the bar doesn’t matter?

  • The amount before the slash is undetermined. I need to order only what is before the slash. What is after either does the order.

  • Don’t have a maximum? It can have 1 million digits?

Show 11 more comments

1 answer

3


This will probably work:

using System;
using System.Collections.Generic;
using System.Linq;
                    
public class Program {
    public static void Main() {
        var lista = new List<string> { "12/10", "01/02", "123/12", "A/1", "4/5" };
        var listaClassificada = lista.OrderBy(x => ConversaoParcial(x));
        listaClassificada.ToList().ForEach(Console.WriteLine);
    }
    public static string ConversaoParcial(string texto) {
        int valor;
        string textoParcial = texto.Split('/', '-')[0];
        return int.TryParse(textoParcial, out valor) ? textoParcial.PadLeft(4, '0') : textoParcial.PadLeft(4, 'A');
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also I put on Github for future reference.

  • Hello bigown, thank you so much for your help. I managed to do the implementation I wanted. Thank you so much. Thanks so much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.