Created
November 15, 2017 00:19
-
-
Save jianminchen/b9d1465e633e1dbbbc41bb8196b6cd18 to your computer and use it in GitHub Desktop.
Order strings - using Linq IList.Sort method
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
/// <summary> | |
/// code study: | |
/// https://www.hackerrank.com/rest/contests/morgan-stanley-codeathon-2017/challenges/shell-sort-command/hackers/ace347/download_solution | |
/// | |
/// Code review: | |
/// Study the post here: | |
/// https://stackoverflow.com/questions/15486/sorting-an-ilist-in-c-sharp | |
/// One blog to read about IList sort better way | |
/// https://www.velir.com/blog/2011/02/17/ilist-sorting-better-way | |
/// </summary> | |
class MorgansS_2 | |
{ | |
static void Main(String[] args) | |
{ | |
int number = Convert.ToInt32(Console.ReadLine()); | |
var orderStrings = new List<string>(number); | |
for (int i = 0; i < number; i++) | |
{ | |
orderStrings.Add(Console.ReadLine()); | |
} | |
var commands = Console.ReadLine().Split(new char[] {' '}); | |
int key = int.Parse(commands[0]); | |
bool reversed = string.Equals(commands[1], "true", StringComparison.Ordinal); | |
bool numeric = string.Equals(commands[2], "numeric", StringComparison.Ordinal); | |
var list = new List<KeyValuePair<string, int>>(number); | |
for (int i = 0; i < number; i++) | |
{ | |
commands = orderStrings[i].Split(new char[] {' '}); | |
list.Add(new KeyValuePair<string, int>(commands[key - 1], i)); | |
} | |
if (numeric) | |
{ | |
list.Sort((x, y) => NumericCompare(x, y)); | |
} | |
else | |
{ | |
list.Sort((x, y) => StringCompare(x.Key, y.Key) ? 1 : (x.Key == y.Key ? 0 : -1)); | |
} | |
if (reversed) | |
{ | |
list.Reverse(); | |
} | |
for (int i = 0; i < number; i++) | |
{ | |
{ | |
Console.WriteLine(orderStrings[list[i].Value]); | |
} | |
} | |
} | |
/// <summary> | |
/// code review on Nov. 14, 2017 | |
/// </summary> | |
/// <param name="a"></param> | |
/// <param name="b"></param> | |
/// <returns></returns> | |
private static bool StringCompare(string a, string b) | |
{ | |
return a.CompareTo(b) < 0? false : true; | |
} | |
/// <summary> | |
/// code review on Nov. 14, 2017 | |
/// | |
/// </summary> | |
/// <param name="first"></param> | |
/// <param name="second"></param> | |
/// <returns></returns> | |
private static int NumericCompare(KeyValuePair<string, int> first, KeyValuePair<string, int> second) | |
{ | |
var orderString1 = first.Key.Trim('0'); | |
var orderString2 = second.Key.Trim('0'); | |
var length1 = orderString1.Length; | |
var length2 = orderString2.Length; | |
if (length1 < length2) | |
{ | |
return -1; | |
} | |
else if (length1 > length2) | |
{ | |
return 1; | |
} | |
if (orderString1 == orderString2) | |
{ | |
return first.Value < second.Value? -1 : 1; | |
} | |
return orderString1.CompareTo(orderString2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment