如何在C#中数字排序分隔字符串数组

时间:2021-10-17 15:52:29

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!

我不知道如何处理这种情况,任何人都可以帮忙吗?谢谢!

5 个解决方案

#1


13  

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

调用Array.Sort,但传入IComparer 的自定义实现:

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

你确实说你需要解析字符串 - 所以你有什么特别的理由要在解码之前解析它们吗?

#2


6  

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

#3


1  

Use an ArrayList (http://msdn.microsoft.com/en-us/library/system.collections.arraylist_methods(VS.80).aspx) so you can sort it.

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

#4


1  

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.

如果数组很大,您将需要在一次传递中提取所有初始整数,因此您不会在每次比较时解析字符串。 IMO,你真的想首先将字符串中编码的信息封装到一个类中。然后对这些对象的数组进行排序。

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. 将编码的字符串映射到Person对象的ArrayList。

  3. Then use ArrayList.Sort(IComparer) where your comparer only looks at the Index.
  4. 然后使用ArrayList.Sort(IComparer),其中比较器仅查看索引。

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

这可能比在每次比较中使用解析更好。

#5


0  

for 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));

#1


13  

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

调用Array.Sort,但传入IComparer 的自定义实现:

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

你确实说你需要解析字符串 - 所以你有什么特别的理由要在解码之前解析它们吗?

#2


6  

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

#3


1  

Use an ArrayList (http://msdn.microsoft.com/en-us/library/system.collections.arraylist_methods(VS.80).aspx) so you can sort it.

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

#4


1  

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.

如果数组很大,您将需要在一次传递中提取所有初始整数,因此您不会在每次比较时解析字符串。 IMO,你真的想首先将字符串中编码的信息封装到一个类中。然后对这些对象的数组进行排序。

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. 将编码的字符串映射到Person对象的ArrayList。

  3. Then use ArrayList.Sort(IComparer) where your comparer only looks at the Index.
  4. 然后使用ArrayList.Sort(IComparer),其中比较器仅查看索引。

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

这可能比在每次比较中使用解析更好。

#5


0  

for 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));