fbpx

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…

av Tornevall

Fotograf, musiker, filmare. Estetikens alla nyanser i ett, kombinerat med humor och ett förflutet inom vård- nöjes- och programmeringsbranscher.