0

I would like a structure to automatically sort data for me based on an associated key, but once this is done I never have to grab any of the objects with the key, I just want to take the first one off the list. In my specific case each object has an associated float value, and I want to order them from low to high.

For example I want to be able to sort a list of integers, but by their corresponding float 'keys' and grab the one at index 0 - (which will be the one with the lowest associated float)

I came across orderedDictionary's but I don't fully understand them and don't know how appropriate they are for my needs. I thought they were just a dictionary that allowed you to also index into them, but they aren't a template class?

4
  • Will you add new items to the list once it is filled once? Commented Jun 7, 2012 at 18:18
  • 3
    What don't you understand about SortedList and OrderedDictionary? You have not explained your use case completely. Commented Jun 7, 2012 at 18:19
  • So if you have a bunch of <int,float> pairs, you want to sort by the float but also have fast access by int, right? Commented Jun 7, 2012 at 18:23
  • Yes I will be adding new items, but I don't need to access by int - just grab the one with the lowest associated float value. Commented Jun 8, 2012 at 7:13

2 Answers 2

3

You probably want a SortedSet: http://msdn.microsoft.com/en-us/library/dd412070.aspx

If you are not using .net 4.0, its available in the PowerCollection project: http://powercollections.codeplex.com/

Example with .Net4.0 SortedSet

SortedSet<float> set = new SortedSet<float>( );
set.Add(13.3f);
set.Add(0.5f);
set.Add(5.5f);

Console.WriteLine(string.Format("Minimum Value: {0}", set.Min)); // prints 0.5
Console.WriteLine(string.Format("Maximum Value: {0}", set.Max)); // prints 13.3

foreach (float f in set)
{
    Console.WriteLine(f);
}
// prints:
// 0.5
// 5.5
// 13.3

// using custom IComparer<float>, see implementation below
set = new SortedSet<float>(new FloatDescComparere());

set.Add(13.3f);
set.Add(0.5f);
set.Add(5.5f);

Console.WriteLine(string.Format("Minimum Value: {0}", set.Min)); // prints 13.3
Console.WriteLine(string.Format("Maximum Value: {0}", set.Max)); // prints 0.5

foreach (float f in set)
{
    Console.WriteLine(f);
}
// prints:
// 13.3
// 5.5
// 0.5

Desc IComparer:

private class FloatDescComparere : IComparer<float>
{
    public int Compare(float x, float y)
    {
        if (y > x)
            return 1;
        else if (x > y)
            return -1;
        else
            return 0;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain what they are a little?
I Added a little example on how to use the SortedSet from .NET4.0
0

You can use a hash table http://en.wikipedia.org/wiki/Hash_table, put the 'key' in the hash and search your element 'key' in the hash , if the hash has the key the you have that element . You must update each time you add a new element O(1) but you also have a find complexity of O(1).

1 Comment

I totally forgot about that he wanted to be sorted , i answered just for the finding part. Then just sort the elements and apply the hash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.