简体   繁体   中英

How to numerically order array of delimited strings in C#

I'm in a little bit of a bind. I'm working with a legacy system that contains a bunch of delimited strings which I need to parse. Unfortunately, the strings need to be ordered based on the first part of the string. The array looks something like

array[0] = "10|JohnSmith|82";
array[1] = "1|MaryJane|62";
array[2] = "3|TomJones|77";

So I'd like the array to order to look like

array[0] = "1|MaryJane|62";
array[1] = "3|TomJones|77";
array[2] = "10|JohnSmith|82";

I thought about doing a 2 dimensional array to grab the first part and leave the string in the second part, but can I mix types in a two dimensional array like that?

I'm not sure how to handle this situation, can anyone help? Thanks!

Call Array.Sort , but passing in a custom implementation of IComparer<string> :

// Give it a proper name really :)
public class IndexComparer : IComparer<string>
{
    public int Compare(string first, string second)
    {
        // I'll leave you to decide what to do if the format is wrong
        int firstIndex = GetIndex(first);
        int secondIndex = GetIndex(second);
        return firstIndex.CompareTo(secondIndex);
    }

    private static int GetIndex(string text)
    {
        int pipeIndex = text.IndexOf('|');
        return int.Parse(text.Substring(0, pipeIndex));
    }
}

Alternatively, convert from a string array into an array of custom types by splitting the string up appropriately. This will make life easier if you're going to do further work on the array, but if you only need to sort the values, then you might as well use the code above.

You did say that you need to parse the strings - so is there any particular reason why you'd want to parse them before sorting them?

        new[] {
            "10|JohnSmith|82",
            "1|MaryJane|62",
            "3|TomJones|77",
        }.OrderBy(x => int.Parse(x.Split('|')[0]));

使用ArrayList( http://msdn.microsoft.com/en-us/library/system.collections.arraylist_methods ( VS.80 ) .aspx )以便对其进行排序。

If the array is large, you will want to extract the initial integers all in one pass, so you are not parsing strings at every comparison. IMO, you really want to encapsulate the information encoded in the strings into a class first. Then sort the array of those objects.

Something like:

class Person {
  int Index { get; }
  string Name { get; }
  int Age { get; }  // just guessing the semantic meaning
}

So then:

  1. Map your encoded string into an ArrayList of Person objects.
  2. Then use ArrayList.Sort(IComparer) where your comparer only looks at the Index.

This will likely perform better than using parse in every comparison.

对于lolz

Array.Sort(array, ((x,y) => (int.Parse(x.Split('|')[0]) < int.Parse(y.Split('|')[0])) ? -1 : (int.Parse(x.Split('|')[0]) > int.Parse(y.Split('|')[0])) ? 1 : 0));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM