How to implement IComparable to sort numerical and non-numerical string


How to implement IComparable to sort numerical and non-numerical string



How to implement IComparable to sort numerical and non-numerical string.

Firstly, I want to get the Min and Max value in the "list".

It is asking me to implement ICompareable. " At least one object must implement IComparable"

Can anyone help me? Here is my code:

        // setup         string filePath1 = Directory.GetCurrentDirectory().ToLower().Replace(@"\bin\debug", string.Empty)             + @"\SampleData\products.xml";          // load XML         var xdoc1 = XElement.Load(filePath1);           //list         var elements = xdoc1.Descendants("ProductName").OrderBy(x => x.Value);          //output         Console.WriteLine("Min- " + elements.Min().Value);         Console.WriteLine("Max- " + elements.Max().Value); 


Here is my XML file:

<Products>   <ProductName>9954</ProductName>   <ProductName>987</ProductName>   <ProductName>197</ProductName>   <ProductName>56</ProductName>   <ProductName>rr45</ProductName>   <ProductName>ad4</ProductName>   <ProductName>7</ProductName>   <ProductName>69dd</ProductName>   <ProductName>69</ProductName>   <ProductName>197abc</ProductName> </Products> 



In Common Lisp, why do multi-expression bodies of (if) statements require (progn)?

1:



The Clojure (or Lisp) Equivalent of a Compound Boolean Test
You're trying to get the Min() and Max() elements To be able to call Min() on it, XElement should implement IComparable.


What is the most impressive Lisp application? [closed]
Since it does not, you can't directly call Min on a Element collection.


What is wrong with the following Common Lisp macro using gensym?
.
LET versus LET* in Common Lisp
Why don't you try to sort your elements by value and take the first and last?.
Automatically create ASDF files for a Common Lisp project
var ordered = elements.OrderBy(e => e.Value).ToList(); var min = ordered.First().Value; var max = ordered.Last().Value; 


Is it feasible to do (serious) web development in Lisp? [closed]


How can ECL include ASDF dependencies in an executable?

2:


It is because you are asking for the minimum and maximum XElement object and the framework doesn't know how to compare XElements as it doesn't implement IComparable.

var elements really evaluates to IEnumerable<XElement> elements.. Also note that your code is not very efficient as you are first sorting then looping through the list twice more, once for Min and once for Max.. Yann's solution is both more efficient and will work:.
var ordered = elements.OrderBy(e => e.Value).ToList();       var min = ordered.First().Value;       var max = ordered.Last().Value; 



74 out of 100 based on 84 user ratings 734 reviews

@