我怎么排序列表 (How do I sort a List> in cust

2019-09-28 08:17发布

我有一个XML,我想的是具有相同的价值组多个元素。

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

我已经分组的值,但它是在List<IGrouping<string, XElement>>格式,这不是问题。

问题是,我想您在列表中的值,看看是否一个特定的值存在,比方说,在这种情况下,“三题”。 所以,无论何时,在任何XML,如果有第三个标题,它总是会在顶部,即[0],其余的会来,因为它们发生。

我想知道如何自定义排序名单。

问候阿曼

Answer 1:

使用带有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());

        }
    }

}

移动元件,而不词典


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);
            }

        }
    }

}


Answer 2:

不知道你的情况的语法,但这里是一个字符串列表,你应该能够很容易适应的解决方案。

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


文章来源: How do I sort a List> in custom order?