Skip to content

Instantly share code, notes, and snippets.

@bpmct
Created February 17, 2020 12:44
Show Gist options
  • Save bpmct/d3b321725d3c8a8402eed498e641f39e to your computer and use it in GitHub Desktop.
Save bpmct/d3b321725d3c8a8402eed498e641f39e to your computer and use it in GitHub Desktop.
using System;
namespace numberSorter
{
class Program
{
// the definition of the delegate function data type
delegate string sortingFunction(string[] a);
static void Main(string[] args)
{
// create an array for the sorted list
string[] aSorted;
// declare the delegate variable which will point to the function to be called
sortingFunction findHiLow;
// a label to allow us to easily loop back to the start if there are input issues
Console.WriteLine("Enter a list of space-separated words");
// read the space-separated string of numbers
string sSentence = Console.ReadLine();
// split the string into the an array of strings which are the individual numbers
string[] sWords = sSentence.Split(' ');
// initialize the size of the unsorted array to 0
int nUnsortedLength = sWords.Length;
// reset nUnsortedLength back to 0 to use as the index to store the numbers in the unsorted array
nUnsortedLength = 0;
// allocate the size of the sorted array
aSorted = new string[sWords.Length];
// prompt for <a>scending or <d>escending
Console.Write("Ascending or Descending? ");
string sDirection = Console.ReadLine();
// Rough check to see if the user typed... Falls back to descending order if not
// Ideally, we would check for both values and do input validation though
if (sDirection.ToLower().StartsWith("a"))
{
findHiLow = new sortingFunction(FindLowestValue);
}
else
{
findHiLow = new sortingFunction(FindHighestValue);
}
// start the sorted length at 0 to use as sorted index element
int nSortedLength = 0;
// while there are unsorted values to sort
while (sWords.Length > 0)
{
// store the lowest or highest unsorted value as the next sorted value
aSorted[nSortedLength] = findHiLow(sWords);
// remove the current sorted value
RemoveUnsortedValue(aSorted[nSortedLength], ref sWords);
// increment the number of values in the sorted array
++nSortedLength;
}
// write the sorted array of numbers
Console.WriteLine("Here's your sorted list: ");
foreach (string thisWord in aSorted)
{
Console.Write($"{thisWord} ");
}
//Pause the program at the end
Console.ReadLine();
}
// find the string that lies first in the alphabet
static string FindLowestValue(string[] array)
{
// define return value
string returnWord;
// initialize to the first element in the array
// (we must initialize to an array element)
returnWord = array[0];
// loop through the array
foreach (string thisWord in array)
{
//uses the commpareTo to check if each word in the array is the lowest
//compared to the previous lowest
if (thisWord.CompareTo(returnWord) != 0 && thisWord.CompareTo(returnWord) == -1)
{
returnWord = thisWord;
}
}
// return the lowest string
return (returnWord);
}
// find the string that lies last in the alphabet
static string FindHighestValue(string[] array)
{
// define return value
string returnWord;
// initialize to the first element in the array
// (we must initialize to an array element)
returnWord = array[0];
// loop through the array
foreach (string thisWord in array)
{
//uses the commpareTo to check if each word in the array is the highest
//compared to the previous highest
if (thisWord.CompareTo(returnWord) != 0 && thisWord.CompareTo(returnWord) == 1)
{
returnWord = thisWord;
}
}
// return the highest value
return (returnWord);
}
// remove the first instance of a value from an array
static void RemoveUnsortedValue(string removeValue, ref string[] array)
{
// allocate a new array to hold 1 less value than the source array
string[] newArray = new string[array.Length - 1];
// we need a separate counter to index into the new array,
// since we are skipping a value in the source array
int dest = 0;
// the same value may occur multiple times in the array, so skip subsequent occurrences
bool bAlreadyRemoved = false;
// iterate through the source array
foreach (string srcWord in array)
{
// if this is the string to be removed and we didn't remove it yet
if (srcWord.CompareTo(removeValue) == 0 && !bAlreadyRemoved)
{
// set the flag that it was removed
bAlreadyRemoved = true;
// and skip it (ie. do not add it to the new array)
continue;
}
// insert the source string into the new array
newArray[dest] = srcWord;
// increment the new array index to insert the next string
++dest;
}
// set the ref array equal to the new array, which has the target number removed
// this changes the variable in the calling function (aUnsorted in this case)
array = newArray;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment