Sort strings in C #

I have a delimited string that I need to sort. First I need to check if "Francais" is in the line, if so, goes first, and then "England" is next if it exists. After that, everything else is in alphabetical order. Can someone help me? Here is what I still have, without sorting

private string SortFrench(string langs) { string _frenchLangs = String.Empty; string retval = String.Empty; _frenchLangs = string.Join(" ; ",langs.Split(';').Select(s => s.Trim()).ToArray()); if (_frenchLangs.Contains("Francais")) retval += "Francais"; if (_frenchLangs.Contains("Anglais")) { if (retval.Length > 0) retval += " ; "; retval += "Anglais"; } //sort the rest return retval; } 
+4
source share
9 answers

Here is what I came up with. You can change .Sort() to OrderBy(lang => lang) after the selection, but I believe it is cleaner.

 public string SortLanguages(string langs) { List<string> languages = langs.Split(';').Select(s => s.Trim()).ToList(); languages.Sort(); PlaceAtFirstPositionIfExists(languages, "anglais"); PlaceAtFirstPositionIfExists(languages, "francais"); return string.Join(" ; ", languages); } private void PlaceAtFirstPositionIfExists(IList<string> languages, string language) { if (languages.Contains(language)) { languages.Remove(language); languages.Insert(0, language); } } 
+5
source

Someone liked my comment, so I decided that I would go ahead and convert it to your code:

 private string SortFrench(string langs) { var sorted = langs.Split(';') .Select(s => s.Trim()) .OrderByDescending( s => s == "Francais" ) .ThenByDescending( s => s == "Anglais" ) .ThenBy ( s => s ) .ToArray(); return string.Join(" ; ",sorted); } 

My syntax may be a bit inactive as I have been in the Unix world for a while and have not used a lot of LINQ lately, but hope this helps.

+11
source

You must use a custom mapper class

it will allow you to use the built-in sort functions of the collection or linq OrderBy using your own criteria

+6
source

Try the following:

 private string SortFrench(string langs) { string _frenchLangs = String.Empty; List<string> languages = langs .Split(';') .Select(s => s.Trim()) .OrderBy(s => s) .ToList(); int insertAt = 0; if (languages.Contains("Francais")) { languages.Remove("Francais"); languages.Insert(insertAt, "Francais"); insertAt++; } if(languages.Contains("Anglais")) { languages.Remove("Anglais"); languages.Insert(insertAt, "Anglais"); } _frenchLangs = string.Join(" ; ", languages); return _frenchLangs; } 
+3
source

Everything can be done on one line.

 private string SortFrench(string langs) { return string.Join(" ; ", langs.Split(';').Select(s => s.Trim()) .OrderBy(x => x != "Francais") .ThenBy(x => x != "Anglais") .ThenBy(x=>x)); } 
+2
source

Sorting in alphabetical order is simple; adding .OrderBy(s => s) before that .ToArray() . Keyword-based sorting is harder.

The quick and dirty way is to divide into three:

  • Lines containing "Francais": .Where(s => s.Contains("Francais")
  • Lines containing "England": .Where(s => s.Contains("Anglais")
  • Else: .Where(s => !francaisList.Contains(s) && !anglaisList.Contains(s))

Then you can sort each of them in alphabetical order and combine them.

Alternatively, you can implement IComparer using the logic you described:

For lines A and B:

  • If A Contains "Francais"
    • If B contains "Francais", alphabetical order
  • Else
    • If B contains "Francais", B goes first.
    • Else
      • If A contains "England",
        • If B contains "England", alphabetical order
        • Else, A goes first
      • Else, alphabetical order

There may be room for logical reinstallation to simplify this. When all this logic is complete in a class that implements IComparer , you can specify this class to use .OrderBy() to order your query results based on your custom logic.

0
source

You can also use Array.Sort(yourStringArray)

0
source

This way you can set any list of words in front:

 private static string SortFrench(string langs, string[] setStartList) { string _frenchLangs = String.Empty; List<string> list = langs.Split(';').Select(s => s.Trim()).ToList(); list.Sort(); foreach (var item in setStartList){ if (list.Contains(item)) { list.Remove(setFirst); } } List<string> tempList = List<string>(); tempList.AddRange(setStartList); tempList.AddRange(list); list = tempList; _frenchLangs = string.Join(" ; ", list); return _frenchLangs; } 
0
source

This code creates a list of languages, sorts them using custom matching, and then matches the sorted list:

  const string langs = "foo;bar;Anglais;Francais;barby;fooby"; var langsList = langs.Split(';').ToList(); langsList.Sort((s1, s2) => { if (s1 == s2) return 0; if (s1 == "Francais") return -1; if (s2 == "Francais") return 1; if (s1 == "Anglais") return -1; if (s2 == "Anglais") return 1; return s1.CompareTo(s2); }); var sortedList = string.Join(";", langsList); Console.WriteLine(sortedList); 
0
source

Source: https://habr.com/ru/post/1480963/


All Articles