C# sort Arraylist strings alphabetical and on length

后端 未结 5 738
名媛妹妹
名媛妹妹 2021-01-19 05:14

I\'m trying to sort an ArrayList of String.

Given:

{A,C,AA,B,CC,BB}

Arraylist.Sort gives:

5条回答
  •  旧时难觅i
    2021-01-19 05:38

    You could create an IComparable class taking in two Strings and sort them as follows:

    if (a.Length == b.Length)
        return String.Compare(a, b);
    return a.Length.CompareTo(b.Length);
    

    You may also want to handle null cases.

提交回复
热议问题