How do I sort a List> in custom order?

2019-08-13 01:17发布

I have an XML and I want to group multiple elements that have the same value.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE repub SYSTEM "C:\repub\Repub_V1.dtd">
<?xml-stylesheet href="C:\repub\repub.xsl" type="text/xsl"?>
<repub>
<head>
<title>xxx</title>
</head>
<body>
<sec>
<title>First Title</title>
<break name="1-1">
<heading><page num="1"/>First Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Second Title</title>
<break name="2-1">
<heading><page num="1"/>Second Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>First Title</title>
<break name="3-1">
<heading><page num="1"/>Third Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
<sec>
<title>Third Title</title>
<break name="4-1">
<heading><page num="1"/>Fourth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
<break name="5-1">
<heading><page num="1"/>Fifth Heading</heading>
<bl>This is another text</bl>
<p>This is a paragraph</p>
</break>
</sec>
</body>
</repub>

I have already grouped the values but it is in List<IGrouping<string, XElement>> format and that is not the issue.

The issue is that I want to check for the values in the list and see if a particular value exists, say for example, in this scenario, "Third Title". So, whenever, in any XML, if there is Third Title, it will always be at the top, that is [0] and the rest will come as they occur.

I want to know how to custom order my list.

Regards Aman

2条回答
你好瞎i
2楼-- · 2019-08-13 01:37

Use a Dictionary with Xml Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication103
{
    class Program
    {

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, XElement> dic = doc.Descendants("sec")
               .GroupBy(x => (string)x.Element("title"), y => y)
               .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }

}

Moving element without dictionary


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication103
{
    class Program
    {

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement body = doc.Descendants("body").FirstOrDefault();

            List<XElement> results = body.Descendants("sec").Where(x => (string)x.Element("title") == "First Title").ToList();
            if (results != null)
            {
                List<XElement> breaks = results.SelectMany(x => x.Elements("break")).ToList();

                XElement newElement = XElement.Parse(results.FirstOrDefault().ToString());
                newElement.Elements("break").Remove();
                results.Remove();
                newElement.Add(breaks);
                body.AddFirst(newElement);
            }

        }
    }

}
查看更多
smile是对你的礼貌
3楼-- · 2019-08-13 01:45

Not sure about the syntax in your case, but here is a solution for a list of strings that you should be able to easily adapt.

List<string> strings = new List<string>
{
    "First title",
    "Second title",
    "Third title",
    "Fourth title",
    "Fifth title"
};

// Create a mask and convert it to List so we have .IndexOf()
var mask = new [] { "Third title" }.ToList();

// Bring items that match the mask on top and we don't care about the rest.
var sorted = strings.OrderByDescending(s => mask.IndexOf(s));

Console.WriteLine($"Sorted:\n{string.Join(",\n", sorted)}");

/*
Sorted:
Third title,
First title,
Second title,
Fourth title,
Fifth title
*/
查看更多
登录 后发表回答