首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用另一个集合进行排序

使用另一个集合进行排序
EN

Stack Overflow用户
提问于 2013-05-29 02:36:31
回答 3查看 163关注 0票数 2

我有一个独特的排序问题,我有一个对象数组,这些对象按“顺序”给我,但顺序对我来说是未知的,也不是根据对象的任何特定属性进行排序的。

对象如下所示

代码语言:javascript
运行
复制
public class Case
{
    public virtual int Id { get; set; }

    public virtual string PalletId { get; set; }
}

但是,它不是该字段的绝对排序,因为它是相对于集合的原始顺序的。

例如:

原始集合

Id - PalletId

1-5

2-6

3-4

4-5

5-6

6-4

已排序的集合

Id - PalletId

1-5

4-5

2-6

5-6

3-4

6-4

上面的排序集合代表了我需要如何对它们进行排序。请注意,排序集合中的palletid不是按升序或降序排列的,而是按照您在原始集合中看到的顺序(5、6、4)进行排序的。在每个托盘id中,我必须以相同的顺序对id字段进行排序。这是我在原始集合中看到特定托盘id的Id字段的顺序。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-29 02:48:05

在澄清之后,一个简单的GroupBy + SelectMany似乎就能完成这个任务:

代码语言:javascript
运行
复制
var sortedCases = originalCases
    .GroupBy(c => c.PalletId)
    .SelectMany(g => g.OrderBy(c => c.Id)) ;

因为GroupBy()根据this SO answer保留密钥的初始顺序。

票数 4
EN

Stack Overflow用户

发布于 2013-05-29 02:40:45

使用OrderBy

代码语言:javascript
运行
复制
var orderedCases = Cases.OrderByDescending(c => c.PalletId).ThenBy(c => c.Id);
票数 3
EN

Stack Overflow用户

发布于 2013-05-29 05:10:14

这给出了正确的答案,但看起来有点麻烦!

在ID值的顺序是降序的情况下,它给出了与Henk的不同的答案。我认为这可能是操作员想要的,但我不能完全确定。

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;

namespace Demo
{
    class Item
    {
        public int Id;
        public int PalletId;

        public override string ToString()
        {
            return string.Format("{0}, {1}", Id, PalletId);
        }
    }

    class Program
    {
        void run()
        {
            var items = new []
            {
                new Item { Id = 1, PalletId = 5},
                new Item { Id = 2, PalletId = 6},
                new Item { Id = 3, PalletId = 4},
                new Item { Id = 4, PalletId = 5},
                new Item { Id = 5, PalletId = 6},
                new Item { Id = 6, PalletId = 4}
            };

            sortItems(items);
            items.Print();
        }

        void sortItems(Item[] items)
        {
            for (int i = 0, j = 1; i < items.Length && j < items.Length; i = j, j = i + 1)
            {
                while ((j < items.Length) && (items[i].PalletId == items[j].PalletId))
                    ++j;

                for (int k = j+1; k < items.Length; ++k)
                {
                    if (items[k].PalletId == items[i].PalletId)
                    {
                        move(items, j, k);
                        break;
                    }
                }
            }
        }

        void move(Item[] items, int to, int from)
        {
            var temp = items[from];

            for (int i = from; i > to; --i)
                items[i] = items[i-1];

            items[to] = temp;
        }

        static void Main()
        {
            new Program().run();
        }
    }

    static class DemoUtil
    {
        public static void Print(this object self)
        {
            Console.WriteLine(self);
        }

        public static void Print(this string self)
        {
            Console.WriteLine(self);
        }

        public static void Print<T>(this IEnumerable<T> self)
        {
            foreach (var item in self) Console.WriteLine(item);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16799068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档