float[float] aa = [2.2:7.7, 3.3:6.6, 1.1:4.4];
std.sort(aa);
assert(aa == [1.1:4.4, 2.2:7.7, 3.3:6.6]);
The above doesn’t work. How does one sort aa in place ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
D’s built-in associative arrays are hash tables. They’re unsorted, and it makes no sense to sort them. The only time that sorting would make any sense would be when iterating over the AA, and to do that requires putting them in a new container. So, you could do something like
but you can’t sort the AA itself. If you want a sorted map, then you need to use something like
std.container.RedBlackTree– though it does take a little bit of work to make it function as a map rather than a set (e.g. the sort function must sort on the key only, and when passing stuff to some functions, you need a tuple with a dummy value).This is why Java has a
HashMapand aSortedMapand why C++ hasunordered_map(C++11) andmap. They both are maps both they have very different characteristics – particularly with regards to sorting and lookup times.