Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello!
How do I order linked list,but this linked list did't order!
C#
LinkedList<double> arrayresultMemberShip = new LinkedList<double>();
arrayresultMemberShip.AddLast(1);
arrayresultMemberShip.AddLast(0.49);
arrayresultMemberShip.OrderBy(i => i);
please help me how used OrderBy?
Posted
Updated 23-Nov-11 7:47am
v4
Comments
Sergey Alexandrovich Kryukov 23-Nov-11 13:55pm    
Not clear what kind of help do you need.
--SA

OrderBy returns a new value that points to the ordered collection; it does not reorder the collection in place:

C#
LinkedList<double> arrayresultMemberShip = new LinkedList<double>();
arrayresultMemberShip.AddLast(1);
arrayresultMemberShip.AddLast(0.49);
arrayresultMemberShip.OrderBy(i => i);

Console.WriteLine("---- Original ----");
foreach (double d in arrayresultMemberShip)
{
    Console.WriteLine(d);
}

IOrderedEnumerable<double> orderedList = arrayresultMemberShip.OrderBy(i => i);

Console.WriteLine("---- Ordered ----");
foreach (double d in orderedList)
{
    Console.WriteLine(d);
}
 
Share this answer
 
Comments
xxxx man 23-Nov-11 14:36pm    
why used IOrderedEnumerable and (i => i)?
Sergey Alexandrovich Kryukov 23-Nov-11 14:43pm    
I tried to answer, please see my solution.
--SA
Jimmanuel 23-Nov-11 20:59pm    
IOrderedEnumerable because that's what OrderBy returns. Read the docs.

(i => i) is the sort function passed to OrderBy that tells it how to sort the elements in teh Linked List. When you access an element it gives you double you put in for that element so this will tell OrderBy to sort the values (1, 0.49).
Sergey Alexandrovich Kryukov 23-Nov-11 14:44pm    
Correct, by OP needs just explanations as it was apparent from the original question. I voted 4.
--SA
I hope you saw the MSDN help page and code sample: http://msdn.microsoft.com/en-us/library/bb534966.aspx[^].

I suspect you don't understand (generics) delegates, lambda syntax (yes, for this simple application understanding syntax is quite enough) or maybe IEnumerable.
Lets see. You are required to supply a parameter, which is the method with the signature defined by the delegate type Func<TSource, TKey>. In your case, this is a function which accept one parameter which is of the type of your list element (double) and returns some key of the type for which the ordering operation is defined. So, your code sample is correct, if you need to return a collection in natural order.

Without the lambda, you could write equivalent code:
C#
static double GetOrderingCriterionKey(double value) { return value; }

//...
IOrderedEnumerable<double> =
   arrayresultMembership.OrderBy(new Func<double,>(GetOrderingCriterionKey));


If you need to change the order, use other OrderBy methods, those using IComparer, see http://msdn.microsoft.com/en-us/library/he2s3bh7%28v=VS.100%29.aspx[^].

See also the definition of the generic delegate type Func. In your case, it is instantiated as
C#
delegate double DoubleFunction(double element);


About IEnumerable and IOrderedEnumerable, just read and understand this: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx[^],
http://msdn.microsoft.com/en-us/library/bb534852.aspx[^].

—SA
 
Share this answer
 
v3
Comments
xxxx man 23-Nov-11 14:52pm    
Thank you very mach but ,I saw Jimmanuel answer before you.
can you explained, why put (i => i)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900