Hacker News new | past | comments | ask | show | jobs | submit login

> How often do you need to sort an array of numbers? In practice you almost always want to sort an array of objects based on one of their properties.

And? If your objects are immovable then one level of indirection (pointers or indices) solves that. If your objects are moveable, then there's many (!) times where having your container sorted will improve performance.




I don't see how you can add that level of indirection if you can only sort an array of numbers. Let's say you have a Person object with property 'age' which is an Int. I can put the Person's ages into an Int Array and sort that. Where to add this indirection to get to the sorted array of Person objects?


1. Have an immutable container of Persons named `persons`

2. Have another container named `indices` representing the index or iterator into your Persons container

In C++:

3. `std::sort(begin(indices), end(indices), [&](int lhs, int rhs)->bool{ return persons[lhs].age() < persons[rhs].age(); });`

I don't know Java but I am sure there are many ways to do it in Java.


Eh yes, this works, but you are not using Arrays.intSort here. Which means that the optimizations that the article is about won't apply. intSort (or any other of the sorts in the article) works purely on an Array of Ints, moving them around and comparing them to each other.

It's quite hard to come up with something that we could use in every day programming that will benefit from the new optimizations.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: