Skip to content

Instantly share code, notes, and snippets.

@icodebuster
Created November 10, 2014 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icodebuster/1a7dd18dfd7e61366c55 to your computer and use it in GitHub Desktop.
Save icodebuster/1a7dd18dfd7e61366c55 to your computer and use it in GitHub Desktop.
Custom String Sorting C#
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