PHP-Sort array based on another array?

For a detailed answer, why array_multisort does not match your needs, view this answer, please: PHP array_multisort not sorting my multidimensional array as expected In short: You want to sort an array based on a predefined order. The Answer is also given over there, but i copied one solution to this answer, too: Use usort … Read more

How to sort an array of objects by date?

As has been pointed out in the comments, the definition of recent isn’t correct javascript. But assuming the dates are strings: var recent = [ {id: 123,age :12,start: “10/17/13 13:07”}, {id: 13,age :62,start: “07/30/13 16:30”} ]; then sort like this: recent.sort(function(a,b) { return new Date(a.start).getTime() – new Date(b.start).getTime() }); More details on sort function from … Read more

Use LINQ to move item to top of list

What do you want to order by, other than the known top item? If you don’t care, you can do this: var query = allCountries.OrderBy(x => x.id != 592).ToList(); Basically, “false” comes before “true”… Admittedly I don’t know what this does in LINQ to SQL etc. You may need to stop it from doing the … Read more

Rails: Order with nulls last

I’m no expert at SQL, but why not just sort by if something is null first then sort by how you wanted to sort it. Photo.order(‘collection_id IS NULL, collection_id DESC’) # Null’s last Photo.order(‘collection_id IS NOT NULL, collection_id DESC’) # Null’s first If you are only using PostgreSQL, you can also do this Photo.order(‘collection_id DESC … Read more