Click here to Skip to main content
15,879,239 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Efficiently sort the following? Pin
arnold_w24-Aug-17 10:48
arnold_w24-Aug-17 10:48 
AnswerRe: Efficiently sort the following? Pin
Richard MacCutchan24-Aug-17 21:05
mveRichard MacCutchan24-Aug-17 21:05 
AnswerRe: Efficiently sort the following? Pin
Richard Deeming25-Aug-17 1:47
mveRichard Deeming25-Aug-17 1:47 
GeneralRe: Efficiently sort the following? Pin
arnold_w28-Aug-17 0:40
arnold_w28-Aug-17 0:40 
GeneralRe: Efficiently sort the following? Pin
Richard Deeming29-Aug-17 1:53
mveRichard Deeming29-Aug-17 1:53 
GeneralRe: Efficiently sort the following? Pin
arnold_w30-Aug-17 4:04
arnold_w30-Aug-17 4:04 
GeneralRe: Efficiently sort the following? Pin
Richard Deeming30-Aug-17 4:40
mveRichard Deeming30-Aug-17 4:40 
AnswerRe: Efficiently sort the following? Pin
Alan N30-Aug-17 5:48
Alan N30-Aug-17 5:48 
Let's do this the old way!

Sorting on multiple keys with a comparer method is straightforward (and easy to understand?). It is just a nested set of comparisons, compare the first key, if equal compare the second key, etc.

For example let's say we have a list of strings and we want a custom sort.
1) by string length
2) alphabetically within each group of equal length

The comparer method is
C#
private static Int32 SortByLengthThenAlpha(String x, String y) {
  // 1st sort
  Int32 result = x.Length.CompareTo(y.Length);
  // result will be < 0, 0, > 0

  if (result == 0) {
    // 2nd Sort, standard string sort
    result = x.CompareTo(y);

    // and so on if more is needed
    // if (result==0) {
    // }
    //
  }
  return result;
}

With an array of strings we can perform the sort with
C#
Array.Sort(strings, SortByLengthThenAlpha);

Original Order
==============
Reddish
Yellowish
Redder
Greenish
Red
Orange
Yellow
Orangy
Green

Sorted
======
Red
Green
Orange
Orangy
Redder
Yellow
Reddish
Greenish
Yellowish

Hope that helps explain the general idea. In your case the keys are the numeric values of T, H and L.

Alan.
GeneralRe: Efficiently sort the following? Pin
arnold_w31-Aug-17 23:50
arnold_w31-Aug-17 23:50 
GeneralRe: Efficiently sort the following? Pin
Alan N3-Sep-17 1:13
Alan N3-Sep-17 1:13 
GeneralRe: Efficiently sort the following? Pin
arnold_w3-Sep-17 2:37
arnold_w3-Sep-17 2:37 
QuestionHow to use tensorflow in c# Pin
delphix518-Jul-17 0:48
delphix518-Jul-17 0:48 
AnswerRe: How to use tensorflow in c# Pin
Richard MacCutchan18-Jul-17 2:09
mveRichard MacCutchan18-Jul-17 2:09 
QuestionC++ algorithm Question Pin
Member 1310646721-Jun-17 18:29
Member 1310646721-Jun-17 18:29 
AnswerRe: C++ algorithm Question Pin
OriginalGriff21-Jun-17 18:37
mveOriginalGriff21-Jun-17 18:37 
QuestionInvoice / Payments history table or statement of account Pin
Bastien Vandamme14-Jun-17 15:50
Bastien Vandamme14-Jun-17 15:50 
AnswerRe: Invoice / Payments history table or statement of account Pin
Gerry Schmitz14-Jun-17 16:17
mveGerry Schmitz14-Jun-17 16:17 
GeneralRe: Invoice / Payments history table or statement of account Pin
Bastien Vandamme14-Jun-17 16:46
Bastien Vandamme14-Jun-17 16:46 
GeneralRe: Invoice / Payments history table or statement of account Pin
Gerry Schmitz14-Jun-17 17:39
mveGerry Schmitz14-Jun-17 17:39 
AnswerRe: Invoice / Payments history table or statement of account Pin
jschell10-Jul-17 4:30
jschell10-Jul-17 4:30 
QuestionChaotic image encryption algorithm by Xingyuan Wang Pin
Member 1325934114-Jun-17 4:53
Member 1325934114-Jun-17 4:53 
QuestionSelecting values from list to make up totals in second list Pin
Wayne Gaylard15-May-17 21:45
professionalWayne Gaylard15-May-17 21:45 
AnswerRe: Selecting values from list to make up totals in second list Pin
Richard MacCutchan15-May-17 22:27
mveRichard MacCutchan15-May-17 22:27 
GeneralRe: Selecting values from list to make up totals in second list Pin
Wayne Gaylard15-May-17 22:34
professionalWayne Gaylard15-May-17 22:34 
AnswerRe: Selecting values from list to make up totals in second list Pin
F-ES Sitecore22-May-17 4:48
professionalF-ES Sitecore22-May-17 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.