Created
November 10, 2014 20:21
-
-
Save icodebuster/1a7dd18dfd7e61366c55 to your computer and use it in GitHub Desktop.
Custom String Sorting C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string[] customSort(string[] unsortedArray) | |
{ | |
var onlyNumber = new List<string>(); | |
var onlyAlpha = new List<string>(); | |
var startAlpha = new List<string>(); | |
var startNum = new List<string>(); | |
// Fisrt we will sort the array using the inbuild array sort function | |
Array.Sort(unsortedArray); | |
// Check for | |
foreach (var value in unsortedArray) | |
{ | |
// Only Numbers | |
if (Regex.IsMatch(value, @"^\d+$")) | |
{ | |
onlyNumber.Add(value); | |
} | |
// Only Alpha | |
else if (Regex.IsMatch(value, @"^[a-zA-Z]+$")) | |
{ | |
onlyAlpha.Add(value); | |
} | |
// Starts With Number | |
else if (Regex.IsMatch(value, @"^\d")) | |
{ | |
startNum.Add(value); | |
} | |
else | |
{ | |
startAlpha.Add(value); | |
} | |
} | |
// Priscy remove this if not requried. | |
//var finalList = new List<string>(); | |
//finalList = onlyAlpha.Concat(startAlpha).ToList().Concat(startNum).ToList().Concat(onlyNumber).ToList(); | |
string[] sortedList = onlyAlpha.Concat(startAlpha).ToList().Concat(startNum).ToList().Concat(onlyNumber).ToList().ToArray(); | |
return sortedList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment