1
kivisi
4y

What is the fastest way to sort an array of dates in java?

Comments
  • 2
    Are you looking for best average time complexity or for what's the fastest to write as code?
  • 0
    Best average time complexity
  • 1
    I think it doesn't take a large time to convert a date to milliseconds. Once its in millis, variation of quicksort heapsort or other fast sorts would suffice
  • 2
    Convert to unix timestamp and just .sort

    Edit, i think the timestamp is even a class member, but correct me if im wrong
  • 0
    @StopWastingTime
    I think Java should use Timsort in its sort-method.
    Its a combination of Insertion-Sort and Mergesort.
    It sorts size>=64 with Insertion-sort, else it splits the collection, sort the sub-collections and merges them.
  • 0
    @metamourge Why is it better than quicksort in yor opinion?
  • 1
    Implement comparator interface and then use Collections.sort()?
  • 1
    Average time complexity for what list length? Short lists up to a hundred elements, or long ones with millions?
  • 1
    While (!array.isSorted()) {
    array.shuffle();
    }

    But more seriously, it is very rare that you need to do it yourself. Use tools provided by language/framework. They will usually ne netter than your code. Of cause exceptions exist.

    For the list less than 1000 items, even bubble sort will be fast enough.

    LinQ Order by proved to be good up to 100.000 items in the list (Around 1ms)
  • 0
    @NoToJavaScript Never use bubble sort, like ever.
  • 3
    "Hello students! Today we're going to learn about BubbleSort."
    "But @Gregozor2121 from the Internet said to never use it!"
    "Yeah he's right. Fuck that shit. That's an A+ for you"
  • 1
    @Gregozor2121 sort a 2 element list without bubble sort
  • 0
    @yellow-dog Oh no! Definitions my only weakness! Is simple if else a sorting algorithm? It sorts! But it isnt scalable! But it sorts! But it is just a simple function!

    It depends:
    If scalability dosent matter then every sorting algo uses bubble sort.
    It it does then it can be solved using a single function that isnt a sorting algo because it isnt scalable.
  • 1
    @don-rager
    That is funny cuz i have heard that from the uni prof...
    Some thing about shit performance and only being simple to write that was its only advantage...

    Bubble sort shouldnt be used, number of cases where it can be used is small and it always can be replaced by a better one. Most launguages use other sorts in their implemetation anyway so i dont see a point in that discussion.

    Or maybe that was a joke that i didnt get...
  • 1
Add Comment