C#中:通用排序的容器,可以返回新添加的对象的排序位置? [英] C#: Generic sorted container that can return the sorted position of a newly added object?

查看:298
本文介绍了C#中:通用排序的容器,可以返回新添加的对象的排序位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要,保持它的元素进行排序,并且可以被询问,其中(在该位置),将插入一个新元素,而不实际插入它一般容器

I need a generic container that keeps its elements sorted and can be asked where (at which position) it would insert a new element, without actually inserting it.

难道这样的容器在.NET库是否存在?
最好的例证就是一个例子(容器由ASCII值排序字符,让我们假设的unicode不存在):

Does such a container exist in the .NET libraries? The best illustration is an example (container sorts characters by ASCII value, let's assume unicode does not exist):

sortedContainer.Add('d');
sortedContainer.Add('b');
sortedContainer.Add('g');

//container contains elements ordered like 'b' 'd' 'g'
//index  -------------------------------->  0   1   2

sortedContainer.GetSortedIndex('a'); //returns 0
sortedContainer.GetSortedIndex('b'); //returns 0

sortedContainer.GetSortedIndex('c'); //returns 1
sortedContainer.GetSortedIndex('d'); //returns 1

sortedContainer.GetSortedIndex('e'); //returns 2
sortedContainer.GetSortedIndex('f'); //returns 2
sortedContainer.GetSortedIndex('g'); //returns 2

sortedContainer.GetSortedIndex('h'); //returns 3
[...]

有关位置的搜索应该充分利用这一事实的元素进行排序。

The search for the position should take advantage of the fact that the elements are sorted.

推荐答案

如果您排序的 列表< T> ,然后使用的 列表< T> .BinarySearch 它会给你的项目的索引,如果它存在,或者它的的指数的位元补会的插入如果插入,然后排序。 。从这一点,你应该能够轻松地建立自己的方法。

If you sort a List<T> and then use List<T>.BinarySearch it will give you the index of the entry if it exists, or the bitwise complement of the index of where it would be inserted if you inserted then sorted. From that, you should easily be able to build your method.

示例代码匹配你的榜样,而不是结果 - 如果你看一下你的样品,你只是完成有3项,所以它不会使'H'返回4或G返回3.我希望感觉这就是你的例子是微客,而不是我误解了问题:)注意排序ISN第•自动 - 你必须调用GetSortedIndex之前显式排序列表

Sample code matching your example, but not the results - if you look at your sample, you've only got 3 entries, so it doesn't make sense for 'h' to return 4 or 'g' to return 3. I hope that's your example which is slightly off, rather than me misunderstanding the problem :) Note that the sorting isn't automatic - you'd have to sort the list explicitly before calling GetSortedIndex.

using System;
using System.Collections.Generic;

static class Test
{
    static int GetSortedIndex<T>(this List<T> list, T entry)
    {
        int index = list.BinarySearch(entry);
        return index >= 0 ? index : ~index;
    }

    static void Main()
    {
        List<char> container = new List<char> { 'b', 'd', 'g' };
        Console.WriteLine(container.GetSortedIndex('a'));
        Console.WriteLine(container.GetSortedIndex('b'));
        Console.WriteLine(container.GetSortedIndex('c'));
        Console.WriteLine(container.GetSortedIndex('d'));
        Console.WriteLine(container.GetSortedIndex('e'));
        Console.WriteLine(container.GetSortedIndex('f'));
        Console.WriteLine(container.GetSortedIndex('g'));
        Console.WriteLine(container.GetSortedIndex('h'));
    }
}

这篇关于C#中:通用排序的容器,可以返回新添加的对象的排序位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆