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

Algorithms

 
AnswerRe: Conditional assembly Pin
Gerry Schmitz28-Sep-17 10:33
mveGerry Schmitz28-Sep-17 10:33 
QuestionCompare similarity of values that can have different lenght, number of words between 2 or more documents. Pin
Member 1342117220-Sep-17 21:46
Member 1342117220-Sep-17 21:46 
AnswerRe: Compare similarity of values that can have different lenght, number of words between 2 or more documents. Pin
Gerry Schmitz21-Sep-17 12:49
mveGerry Schmitz21-Sep-17 12:49 
QuestionEfficiently sort the following? Pin
arnold_w24-Aug-17 10:18
arnold_w24-Aug-17 10:18 
GeneralRe: Efficiently sort the following? Pin
harold aptroot24-Aug-17 10:28
harold aptroot24-Aug-17 10:28 
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 
I'm not sure it's particularly efficient, but using LINQ, you can at least get the right answer. Smile | :)
C#
struct Record
{
    public int T { get; set; }
    public int H { get; set; }
    public int L { get; set; }
}

Record[] UnsortedRecords =
{
    new Record { T = 0, H = 0, L = 2 },
    new Record { T = 1, H = 2, L = 1 },
    new Record { T = 0, H = 1, L = 4 },
    new Record { T = 0, H = 0, L = 0 },
    new Record { T = 2, H = 4, L = 2 },
    new Record { T = 2, H = 3, L = 6 },
};

public static IEnumerable<Record> SortRecords(IEnumerable<Record> source)
{
    return source
        .GroupBy(r => r.T, (key, items) => items
            .GroupBy(r => r.H)
            .SelectMany(g => g)
        )
        .SelectMany(g => g);
}

IEnumerable<Record> SortedRecords = SortRecords(UnsortedRecords);

/*
Sorted records:

T0 H0 L2 
T0 H0 L0 
T0 H1 L4 
T1 H2 L1 
T2 H4 L2 
T2 H3 L6 
*/

NB: The documentation for GroupBy states that the order of the input sequence is preserved by both the returned groups, and the items within each group:
The IGrouping<TKey,TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey,TElement>. Elements in a grouping are yielded in the order they appear in source.


You might be tempted to use a composite key in the grouping, to reduce the nesting. But that won't work - T0H1 and T1H2 will be returned in the wrong order:
C#
source.GroupBy(r => new { r.T, r.H }).SelectMany(g => g);

/*
Result:

T0 H0 L2 
T0 H0 L0 
T1 H2 L1 
T0 H1 L4 
T2 H4 L2 
T2 H3 L6 
*/




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


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 
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 

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.