Hem

”How do I sort a HashTable?”

I consider hashtables in C# as a similarity to a normal array in PHP. But it’s not! Hashtables is great to use when it comes to ”temporary storage” of variables, but they are useless if you need to sort the information. Google doesn’t help either. It’s always about half solutions and you have to guess most of the time, how you should solve the problem, and it’s not getting better since I’m lazy. But this was actually solved. I needed to sort a kind of score of sales with the highest value on top, like in top ten. And it was for ASP.NET.

Via google I found out that an ArrayList could fix my issue. The only problem was that the sort was handled as strings, which gave a very ugly result.

So here’s my solution!

Kod:
  // [.. source ..]

  // The first hashtable content
  SalesData.Add(Säljare, SoldItems.ToString());

  // [.. more source ..]

  // Time to sort our data. Create a new hashtable!
  Hashtable SortList = new Hashtable();
  string CountString = "";

  // Scan through the salesmen and collect all values in reversed order (where all salesmen with 30 sold items will put in one
  // hash, 29 in another, and so on...
  foreach (string SalesScorers in SalesData.Keys)
  {
                               // Check the length of the counted items. If the answer is only "1", put a zero before the value
                               // so the outdata will be 01, 02, 03, ... 10, 11, 12, ... 28, 29, 30, and so on.

                               if (SalesData[SalesScorers].ToString().Length == 1) { CountString = "0" + SalesData[SalesScorers].ToString(); } else { CountString = SalesData[SalesScorers].ToString(); }

                               // Then add the new data, with the scores as a key, and the salespeople as the value.
                               // The nice part here is that the salespeople are identified with an id instead of their names, so there
                               // will always only be one space per person.                           if (!SortList.Contains(CountString))
                               {
                                                            SortList.Add(CountString, SalesScorers + " ");
                               }
                               else
                               {
                                                            SortList[CountString] += SalesScorers + " ";
                               }
  }

  // Now, create an arraylist, and sort it by the key.
  ArrayList Lista = new ArrayList(SortList.Keys);
  Lista.Sort();

  // Make the order descending, so the highest value will be put first
  Lista.Reverse();

At this point you can now foreach through all of the salesmen, and split them up into separate peaces, and the output result is beautiful!

There’s probably other ways too, but with HashTables there’s apparently no easy way to solve this, like it is in PHP where you can use the built-in sorting functions…

Personligt brev till HOODEN!

Tjära rökare!

För varje gång ni tänder en cigarett på er balkong eller i närheten av ett fönster riskerar er cigarettrök att nå andra lägenheters fönster, vars hyresgäster mycket väl kan ha astma.

Detta är förstås inte alls bra, eftersom det medför att vi som också vill bo här, måste stänga våra fönster. Ju fler cigaretter ni tänder, desto mer sällan kan vi andra därmed ha våra fönster öppna.

Därför vore det oerhört uppskattat om ni INTE tänder era cigaretter en gång varje halvtimme, då vissa av oss försöker åtnjuta en skön sommar utan att behöva oroa oss för en framtida vistelse på sjukhus på grund av att våra grannar gett oss KOL.

snälla, respektera era grannars trivsel och försök röka lite mindre, åtminstone när det gäller från balkonger eller fönster.”

 

Gissa om snacket går nu?