Sorted Collection in C#
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
-
Veni, vidi, abiit domum
All of the standard sorted collections require unique keys which violates one of OP's requirements.
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
Member 10339829 wrote:
If it is almost sorted, the sorting time must be minimal.
We always strive for minimal time; how many elements are in your list?
Member 10339829 wrote:
The keys in collection must NOT be unique!
Then they're no longer "keys", but simply values. That equates to an array of objects - so, an IComparer would sort that.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
You need a collection of ... what ? And, what is the "sorting algorithm" you require/need/want ? .NET hands you the ability to easily sort (using a QuickSort algorithm) generic Lists of value types by just invoking the 'Sort function: "All numeric types (such as Int32 and Double) implement IComparable, as do String, Char, and DateTime. Custom types should also provide their own implementation of IComparable to enable object instances to be ordered or sorted." And, writing your own IComparable is not that difficult; for example: If you have a TreeNode Collection:
public List<TreeNode> sTNList = new List<TreeNode>
{
new TreeNode("1"),
new TreeNode("1"),
new TreeNode("2"),
new TreeNode("4"),
new TreeNode("1"),
new TreeNode("4"),
new TreeNode("8"),
new TreeNode("2"),
new TreeNode("3")
};You can easily define a custom IComparer Class to sort the nodes:
public class TreeNodeICompare : IComparer<TreeNode>
{
public int Compare(TreeNode x, TreeNode y)
{
// basic alphanumeric sort
return(x.Text.CompareTo(y.Text));
}
}And invoke it:
sTNList.Sort(0, sTNList.Count, new TreeNodeICompare());
See [^] for the documentation on the call to 'Sort used here. And, note: "This method uses Array.Sort, which uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. ... On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation." Using a non-unique property of a collection, like the 'Text property of a TreeNode, you avoid possible duplicate key issues that might arise with other types of collections. Perhaps if you describe the scenario in which your collection is being used you can get more than a "mind-reader's guess" type of response here. bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
what the sort rule? how about the SortedDictionary? http://msdn.microsoft.com/en-us/library/f7fta44c%28v=vs.85%29.aspx[^]
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
If your list is almost sorted, you could use your own sort alogrithm. If your run some tests[^], you will notice bubble sort does fairly well with lists that are almost sorted.
Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
you can getbetter solution by below link Sorting in Collection[^] :thumbsup:
-
I need a collection, which is easy to sort (or sorted collection). If it is almost sorted, the sorting time must be minimal. Or it can be collection, which keeps its elements sorted, even if you change the keys of elements. The keys in collection must NOT be unique! (Maybe some sort of tree collection internally.)
You could use the OrderBy<TSource, TKey>(Func<TSource, TKey>) extension method. The actual sorting is deffered until result gets enumerated, so the call to OrderBy() itself is fast. It performs a stable sort, which means that the order of already sorted elements is preserved. it does not sort the original sequence in-place. Instead, the returned IEnumerable returns elements of the original sequence in sorted order. And you can speed things up a bit with parallel LINQ like:
var result = sourceSequence.AsParallel().OrderBy(item => item.Key);
Gabriel Szabo