Custom sort of List<string>

88 Views Asked by At

I have a List<string>, where the entries are locations, like "010101", "010102", "010201", ..., with following meaning:

  • First two characters : floor
  • Next two characters : lane
  • Last two characters : entry in lane

Normal sorting reveals a list like the following:

010101
010102
010103
010201
010202
020101
020102
020201
020202
020203
020204
...

I would like a custom sorting, where:

  • on floor "01", the sorting is done in a normal way:

      010101
      010102
      010103
      010201
      010202
    
  • on floor "02", the sorting is done in a reversed way for the lane, but not for the entry in lane, so something like:

      ...
      020201
      020202
      020203
      020204
      020101
      020102
    

Here on the site, I've found the IEnumerable as a solution, but this only works for List<T> where T is a new class. This is not relevant here, as I'm dealing with "simple" strings.
I've also found the Enumerable as a solution, but this also doesn't work, because the list of possible strings is far too large (not enumerable) and I'd prefer a flexible solution.

Does anybody have an idea?

Thanks in advance

2

There are 2 best solutions below

2
Stephan Bauer On BEST ANSWER

There are multiple ways to achieve the desired behavior, e.g. using a Comparison<string>:

var input = new List<string>() { "010101", "010102", "010103", "010201", "010202", "020101", "020102", "020201", "020202", "020203", "020204" };

Comparison<string> comparison = (s1, s2) =>
{
    if (s1 == null) throw new ArgumentNullException(nameof(s1));
    if (s2 == null) throw new ArgumentNullException(nameof(s2));
    if (s1.Length != 6 || s2.Length != 6) throw new ArgumentOutOfRangeException();

    // Compare floor (first 2 digits)
    var floor1 = s1.Substring(0, 2);
    var floor2 = s2.Substring(0, 2);
    var result = floor1.CompareTo(floor2);
    if (result != 0) return result;

    // Compare lane (middle 2 digits)
    var lane1 = s1.Substring(2, 2);
    var lane2 = s2.Substring(2, 2);
    // Special case: in floor 02, reverse order of lanes
    result = floor1 == "02"
       ? lane2.CompareTo(lane1) 
       : lane1.CompareTo(lane2);
    if (result != 0) return result;

    // Compare entry (last 2 digits)
    return s1.Substring(4, 2).CompareTo(s2.Substring(4, 2));
};

// Sort the list using the comparison defined before
input.Sort(comparison);
1
Enigmativity On

If your input is nicely curated (i.e. all numerals and all 6 characters) then this is a simple way to do it:

var input = new List<string>()
{
    "010101", "010102", "010103", "010201", "010202",
    "020101", "020102", "020201", "020202", "020203", "020204"
};

var output =
    input
        .Select(x => new
        {
            location = x,
            floor = int.Parse(x.Substring(0, 2)),
            lane = int.Parse(x.Substring(2, 2)),
            entry = int.Parse(x.Substring(4, 2))
        })
        .OrderBy(x =>
            x.floor * 10000
            + (x.floor % 2 == 0 ? 99 - x.lane : x.lane) * 100
            + x.entry)
        .Select(x => x.location)
        .ToList();

That produces:

010101 
010102 
010103 
010201 
010202 
020201 
020202 
020203 
020204 
020101 
020102