Click here to Skip to main content
15,885,874 members
Home / Discussions / C#
   

C#

 
GeneralRe: List problem ! Pin
_Q12_3-Nov-13 19:47
_Q12_3-Nov-13 19:47 
AnswerRe: List problem ! Pin
OriginalGriff3-Nov-13 22:04
mveOriginalGriff3-Nov-13 22:04 
AnswerRe: List problem ! Pin
Richard MacCutchan3-Nov-13 22:10
mveRichard MacCutchan3-Nov-13 22:10 
AnswerRe: List problem ! Pin
Paulo Augusto Kunzel4-Nov-13 3:02
professionalPaulo Augusto Kunzel4-Nov-13 3:02 
AnswerRe: List problem ! Pin
Pete O'Hanlon4-Nov-13 3:24
mvePete O'Hanlon4-Nov-13 3:24 
Questionalways black screen Pin
josephdalebert2-Nov-13 23:33
josephdalebert2-Nov-13 23:33 
AnswerRe: always black screen Pin
Richard Andrew x643-Nov-13 13:20
professionalRichard Andrew x643-Nov-13 13:20 
QuestionC# Generic Dictionary ordered by Value: is it really ordered ? Pin
BillWoodruff2-Nov-13 8:05
professionalBillWoodruff2-Nov-13 8:05 
There are a number of threads on StackOverFlow in which people disagree about the possibility of truly sorting a "standard" .NET Generic Dictionary: i.e., not an instance of the .NET SortedDictionay. Given that a .NET Dictionary is implemented with the use of Hashes for fast look-up, those who believe it cannot be ordered describe that, and the statements by Microsoft about "no guarantee" of a specific order, as the reasons sorting is not possible.

In this code:
XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Nov2_CustomShape
{
    public class RandomizedDictionary
    {
        private Dictionary<int, int> randomDictionary;

        public Dictionary<int, int> sortByKeyAsc;
        public Dictionary<int, int> sortByKeyDesc;
        public Dictionary<int, int> sortByValueAsc;
        public Dictionary<int, int> sortByValueDesc;

        private Random rand1, rand2;

        private int i, j;

        public RandomizedDictionary(int nKeyValuePairs)
        {
            randomDictionary = new Dictionary<int, int>();

            rand1 = new Random((int)DateTime.Now.Ticks);
            Thread.Sleep(100);
            rand2 = new Random((int)DateTime.Now.Ticks);

            while (randomDictionary.Count < nKeyValuePairs)
            {
                i = rand1.Next(0, nKeyValuePairs);
                j = rand2.Next(0, nKeyValuePairs);

                if(randomDictionary.Keys.Contains(i) || randomDictionary.Values.Contains(j)) continue;

                randomDictionary.Add(i,j);
            }

            sortByKeyAsc = randomDictionary.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
            sortByKeyDesc = randomDictionary.OrderByDescending(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
            sortByValueAsc = randomDictionary.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            sortByValueDesc = randomDictionary.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
        }
    }
}
A Dictionary with a specified number of Key-Value Pairs is created where each Key is taken at random from the range #0 to the number of Key-ValuePairs minus one, and the Values are also randomly assigned from the same range: no duplicates are allowed for the Values.'

If you test this:
RandomizedDictionary testDictionarySort = new RandomizedDictionary(1000);
and examine the four sorted Dictionaries exposed by the Class, they appear (so far, in my tests) to be sorted as expected.

I'm curious to know if you agree with the opinion of those that believe you cannot rely on a Generic Dictionary, sorted via Linq, to be, in fact, sorted.

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

AnswerRe: C# Generic Dictionary ordered by Value: is it really ordered ? Pin
harold aptroot2-Nov-13 8:23
harold aptroot2-Nov-13 8:23 
GeneralRe: C# Generic Dictionary ordered by Value: is it really ordered ? Pin
BillWoodruff2-Nov-13 18:43
professionalBillWoodruff2-Nov-13 18:43 
AnswerRe: C# Generic Dictionary ordered by Value: is it really ordered ? Pin
TnTinMn2-Nov-13 17:28
TnTinMn2-Nov-13 17:28 
GeneralRe: C# Generic Dictionary ordered by Value: is it really ordered ? Pin
BillWoodruff2-Nov-13 18:48
professionalBillWoodruff2-Nov-13 18:48 
GeneralRe: C# Generic Dictionary ordered by Value: is it really ordered ? Pin
TnTinMn3-Nov-13 10:50
TnTinMn3-Nov-13 10:50 
AnswerRe: C# Generic Dictionary ordered by Value: is it really ordered ? Pin
jschell4-Nov-13 8:05
jschell4-Nov-13 8:05 
AnswerRe: C# Generic Dictionary ordered by Value: is it really ordered ? Pin
Keith Barrow5-Nov-13 2:22
professionalKeith Barrow5-Nov-13 2:22 
QuestionBurning open file to CD/DVD Pin
Member 103095672-Nov-13 1:34
Member 103095672-Nov-13 1:34 
AnswerRe: Burning open file to CD/DVD Pin
Dave Kreskowiak2-Nov-13 9:20
mveDave Kreskowiak2-Nov-13 9:20 
GeneralRe: Burning open file to CD/DVD Pin
Member 103095672-Nov-13 9:45
Member 103095672-Nov-13 9:45 
AnswerRe: Burning open file to CD/DVD Pin
TnTinMn3-Nov-13 11:58
TnTinMn3-Nov-13 11:58 
QuestionComparing a name inputted against a database of names Pin
JacksonVF1-Nov-13 22:56
JacksonVF1-Nov-13 22:56 
AnswerRe: Comparing a name inputted against a database of names Pin
Mycroft Holmes2-Nov-13 1:21
professionalMycroft Holmes2-Nov-13 1:21 
GeneralRe: Comparing a name inputted against a database of names Pin
JacksonVF2-Nov-13 2:45
JacksonVF2-Nov-13 2:45 
AnswerRe: Comparing a name inputted against a database of names Pin
jschell2-Nov-13 6:49
jschell2-Nov-13 6:49 
AnswerRe: Comparing a name inputted against a database of names Pin
PIEBALDconsult4-Nov-13 4:11
mvePIEBALDconsult4-Nov-13 4:11 
Questionsingle chat TCP, Console C# Pin
Member 103468781-Nov-13 19:01
Member 103468781-Nov-13 19:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.