0

I am using LINQ and want to make a list order descending. I try it like this:

XElement Icecreams =
      new XElement("Icecreams",
      new XElement("Icecream",
      new XComment("Cherry Vanilla Icecream"),
      new XElement("Flavor", "Cherry Vanilla"),
        new XElement("Flavor", "Cherry Vanilla"),
          new XElement("Flavor", "e"),
          new XElement("Flavor", "d"),
          new XElement("Flavor", "gg"),
          new XElement("Flavor", "c"),
          new XElement("Flavor", "b"),
          new XElement("Flavor", "a"),
      new XElement("ServingSize", "Half Cup"),
      new XElement("Price", 10),
      new XElement("Nutrition",
      new XElement("TotalFat", "15g"),
      new XElement("Cholesterol", "100mg"),
      new XElement("Sugars", "22g"),
      new XElement("Carbohydrate", "23g"),
      new XElement("SaturatedFat", "9g"))));

            Icecreams.Add(
            new XElement("Icecream",
            new XComment("Strawberry Icecream"),
            new XElement("Flavor", "Strawberry"),
            new XElement("ServingSize", "Half Cup"),
            new XElement("Price", 10),
            new XElement("Nutrition",
            new XElement("TotalFat", "16g"),
            new XElement("Cholesterol", "95mg"),
            new XElement("Sugars", "22g"),
            new XElement("Carbohydrate", "23g"),
            new XElement("SaturatedFat", "10g"))));


            XElement IcecreamsList = new XElement("IcecreamsList",
               (from c in Icecreams.Elements("Icecream")
                    //where (c.Element("Price").Value == "10")
                orderby c.Element("Flavor").ToString() descending
                select c));




            Icecreams.Save(@"D:/IcecreamsNiceok.xml");

But if I look at the file, then I still see this:

?xml version="1.0" encoding="utf-8"?>
<Icecreams>
  <Icecream>
    <!--Cherry Vanilla Icecream-->
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>e</Flavor>
    <Flavor>d</Flavor>
    <Flavor>gg</Flavor>
    <Flavor>c</Flavor>
    <Flavor>b</Flavor>
    <Flavor>a</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>15g</TotalFat>
      <Cholesterol>100mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>9g</SaturatedFat>
    </Nutrition>
  </Icecream>
  <Icecream>
    <!--Strawberry Icecream-->
    <Flavor>Strawberry</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>16g</TotalFat>
      <Cholesterol>95mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>10g</SaturatedFat>
    </Nutrition>
  </Icecream>
</Icecreams>

So the element with name Flavor is nor ordered.

So my question is: how to make the list ordered descending?

Thank you.

oke, if I do it like this:

       XElement Icecreams =
  new XElement("Icecreams",
  new XElement("Icecream",
  new XComment("Cherry Vanilla Icecream"),
  new XElement("Flavor", "Cherry Vanilla"),
    new XElement("Flavor", "Cherry Vanilla"),
      new XElement("Flavor", "f"),
      new XElement("Flavor", "e"),
      new XElement("Flavor", "d"),
      new XElement("Flavor", "c"),
      new XElement("Flavor", "b"),
      new XElement("Flavor", "a"),
  new XElement("ServingSize", "Half Cup"),
  new XElement("Price", 10),
  new XElement("Nutrition",
  new XElement("TotalFat", "15g"),
  new XElement("Cholesterol", "100mg"),
  new XElement("Sugars", "22g"),
  new XElement("Carbohydrate", "23g"),
  new XElement("SaturatedFat", "9g"))));

        Icecreams.Add(
        new XElement("Icecream",
        new XComment("Strawberry Icecream"),
        new XElement("Flavor", "Strawberry"),
        new XElement("ServingSize", "Half Cup"),
        new XElement("Price", 10),
        new XElement("Nutrition",
        new XElement("TotalFat", "16g"),
        new XElement("Cholesterol", "95mg"),
        new XElement("Sugars", "22g"),
        new XElement("Carbohydrate", "23g"),
        new XElement("SaturatedFat", "10g"))));


        XElement IcecreamsList = new XElement("IcecreamsList",
           (from c in Icecreams.Elements("Icecream")
                //where (c.Element("Price").Value == "10")
            orderby c.Element("Flavor").ToString() ascending
            select c));


        IcecreamsList.Save(@"D:/IcecreamlistOrdered.xml");

So I save the ordered list.The output is still the same:

<?xml version="1.0" encoding="utf-8"?>
<IcecreamsList>
  <Icecream>
    <!--Cherry Vanilla Icecream-->
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>f</Flavor>
    <Flavor>e</Flavor>
    <Flavor>d</Flavor>
    <Flavor>c</Flavor>
    <Flavor>b</Flavor>
    <Flavor>a</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>15g</TotalFat>
      <Cholesterol>100mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>9g</SaturatedFat>
    </Nutrition>
  </Icecream>
  <Icecream>
    <!--Strawberry Icecream-->
    <Flavor>Strawberry</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>16g</TotalFat>
      <Cholesterol>95mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>10g</SaturatedFat>
    </Nutrition>
  </Icecream>
</IcecreamsList>

Because I do ascending, it has to be:

a b c ..

So if I try it like this:

XElement IcecreamsList = new XElement("IcecreamsList",
               (from c in Icecreams.Elements("Icecream")
                    //where (c.Element("Price").Value == "10")
                orderby c.Element("Flavor").Value ascending
                select c));

still the output is like this:

<Flavor>f</Flavor>
    <Flavor>e</Flavor>
    <Flavor>d</Flavor>
    <Flavor>c</Flavor>
    <Flavor>b</Flavor>
    <Flavor>a</Flavor>

I have it like this:

      new XElement("Flavor", "f"),
      new XElement("Flavor", "e"),
      new XElement("Flavor", "d"),
      new XElement("Flavor", "c"),
      new XElement("Flavor", "b"),
      new XElement("Flavor", "a"),

but after I save the file. I want it to have it in ascending order, like this:

new XElement("Flavor", "f"),
          new XElement("Flavor", "a"),
          new XElement("Flavor", "b"),
          new XElement("Flavor", "c"),
          new XElement("Flavor", "d"),
          new XElement("Flavor", "e"),
LikeToDo
  • 319
  • 1
  • 4
  • 13
  • 6
    You dont save the ordered List – Romano Zumbé Jul 17 '17 at 07:58
  • Try using `c.Element("Flavor").Value` instead of `c.Element("Flavor").ToString()`. – Keyur PATEL Jul 17 '17 at 08:12
  • @PATEL. Thank you for your comment. Yes, I thought that would helped. But the ordering is still descending and not ascending. See my post – LikeToDo Jul 17 '17 at 08:17
  • Then either you could switch it to `c.Element("Flavor").Value desecnding` and see what is returned, or perhaps try `c.Element("Flavor").Value.ToString()`. – Keyur PATEL Jul 17 '17 at 08:21
  • Oke, I have tried both:c.Element("Flavor").Value desecnding and c.Element("Flavor").Value.ToString() but still ordered descending – LikeToDo Jul 17 '17 at 08:23
  • Please reduce this to a [mcve]. There's a lot in here that's unnecessary, but you haven't given us something we can copy/paste/compile/run. Please pay attention to the formatting, too - the indentation seems to be pretty arbitrary at the moment. – Jon Skeet Jul 17 '17 at 08:28
  • @Jon Skeet. I realy don't understand what you mean. – LikeToDo Jul 17 '17 at 09:05
  • Which aspect? You only seem to care about the flavours, so all the nutrition part is irrelevant, and you haven't included an example which is completely standalone showing the problem. – Jon Skeet Jul 17 '17 at 09:30
  • @JonSkeet. I still don't understand your problem? I have given you a standalone example. How standalone do you want to have it? Petrosov already have helped me. Because apparently he not only understand the problem, but also can solve the problem, what you apparently can't. Of course that doesn't matter. Everybody has his own skills. So I wish you the best. – LikeToDo Jul 18 '17 at 06:38
  • No, you haven't given a standalone example. I've already explained what I mean by standalone: we should be able to copy/paste/compile/run. So a short console app, complete with `using` directives etc. I also want it to be minimal - remove everything that isn't relevant to the question. Please don't mistake my not answering for me not being able to solve the problem - I'm trying to optimize for the *site* rather than for *you*. The aim of Stack Overflow is to create a repository of high quality questions and answers. This isn't a high quality question, despite me explaining how to make it so. – Jon Skeet Jul 18 '17 at 06:41

2 Answers2

2

Your problem is that you are trying to Order By Flavor value the Icecreams and not the Flavors. Here is example how you can order Flawors in the Icecreams:

foreach(XElement iceCream in Icecreams.Elements("Icecream"))
{
    XElement IcecreamsList = new XElement("IcecreamsList",
            (from c in iceCream.Elements("Flavor")
            orderby c.Value ascending
            select c));
    for (int i = 0; i < iceCream.Elements("Flavor").Count();i++)
    {
        iceCream.Elements("Flavor").ToList()[i].ReplaceWith(IcecreamsList.Elements("Flavor").ToList()[i]);
    }
}

Icecreams.Save(YOUR_PATH);
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
1

You ordered your Icecreams by flavor.

So your ice cream with the flavor "f" is on top of your ice cream with the flavor "Strawberry". I would say that's working as expected. I cannot see anything wrong with the result compared to the code.

Maybe your expectations are off. Did you instead mean to order the flavors inside each ice cream element?

nvoigt
  • 75,013
  • 26
  • 93
  • 142