Is it possible to sort a HashTable?

前端 未结 11 719
不知归路
不知归路 2021-01-17 10:13

I have a property that returns a HashTable. I would like to sort it without refactoring my property. Please note: I do not want to return another type.

11条回答
  •  隐瞒了意图╮
    2021-01-17 10:52

    Not exactly a C# answer but I am sure you can make something of it.

    In Perl it is common to "sort" a hash table for use in output to the display.

    For example:

    print "Items: ";
    foreach (sort keys %items) {
        print $_, '=', $items{$_}, ' ';
    }
    

    The trick here is that Perl doesn't sort the hash, it is sorting a copied list of hash keys. It should be easy enough in C# to extract the hash keys into a list and then sort that list.

提交回复
热议问题