Sort list of nested strings in C #

What I want to achieve is an alphabetically sorted nested list.

eg. my input:

fat, twat, gat //Line 1 cat, hat, bat // Line 2 twat, lack, rat // Line 3 

I want the output to be:

 bat, cat, hat // Line 2 fat, gat, twat // Line 1 lack, rat, twat // Line 3 

As you can see, the list is first sorted internally as well as externally.

My implementation currently uses a nested list:

 List<List<String>> testSort; 

I managed to sort the internal list using this method:

  public static List<List<String>> sortList(List<List<String>> input) { input.ForEach(delegate (List<string> o) { o.Sort(); }); return input; } 

I am not sure how to sort the list outside. Help will be appreciated!

Thanks in advance!

+5
source share
4 answers

You can use OrderBy () to sort the outer list based on the first element of each list after each inner list is already sorted:

 input.ForEach(t => t.Sort()); input = input.OrderBy(t => t.First()).ToList(); 

In response to the comment by M.kazem Akhgary below, I can only think of this solution if you want the sorting of the external list to be not only based on the first element, but also the entire list. Maybe someone has a better solution.

 input.ForEach(t => t.Sort()); input.Sort((e1, e2) => { for (int i = 0; i < e1.Count; i++) { if(e1[i] != e2[i]) { return e1[i].CompareTo(e2[i]); } } return 0; }); 
+8
source

Since the signature of sortList is of return type, it is better not to change the contents of the input list, which can lead to side effects and errors.

Here is how I would decide:

 public static List<List<String>> sortList(List<List<String>> input) { return input .Select(x => x.OrderBy(y => y).ToList()) .OrderBy(x => String.Join(", ", x)) .ToList(); } 

So, given this input:

 var input = new List<List<string>>() { new List<string>() { "fat", "twat", "gat", }, new List<string>() { "cat", "hat", "bat", }, new List<string>() { "twat", "lack", "rat", }, }; 

This will be the output:

output

+4
source

I'm not sure how to sort the list outside now.

Suppose you have sorted all the internal lists, now you can:

 input = input.OrderBy(subList => String.Join(",", subList)).ToList(); 

This will combine the lists into one line for comparison, so that it will compare the second element of the list if the first elements match. And third, if all the other elements are the same ...

In using String.Join() , a separator must be used to avoid accidentally using second elements when comparing the first elements. The separator should not appear in the source line.

+2
source

You can implement your own comparison list. without using Linq

 private static void Main() { List<List<string>> list = new List<List<string>> { new List<string> {"fat", "twat", "gat"}, new List<string> {"cat", "hat", "bat"}, new List<string> {"twat", "lack", "rat"} }; // sort the inner list foreach (var l in list) { l.Sort(); } list.Sort(new ListComparer<string>()); // sort outer list according to ListComparer class. } 

This is a comparison list.

 class ListComparer<T> : Comparer<List<T>> where T : IComparable { // Compare lists elements until they are no more equal. // return result of last compared elements as result. public override int Compare(List<T> x, List<T> y) { int compared = 0; for (int i = 0; compared == 0 && i < x.Count && i < y.Count ; i++) { compared = x[i].CompareTo(y[i]); } return compared; } } 

The Compare method returns an integer indicating whether the first list is greater than ( 1 ) or the second ( -1 ). It will return 0 if both lists are equal.

The Sort method uses these results to sort your list.

+2
source

All Articles