Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java programming question: How do I use insertion sort to sort items in descending order using compareTo?
I have no idea where to start.
Usually, sorting primitive data types,
I would only need to use <, > but compareTo only gives me if it's less than, equal, or greater than each other.
Would I need to implement a while-loop within a for-loop?
2 Answers
- husoskiLv 74 years ago
Write an insertion sort to put the items in increasing order. There are samples all over the web for that. Wikipedia's article is pretty good:
https://en.wikipedia.org/wiki/Insertion_sort#Algor...
Pseudocode and C source are shown.
When that works, reverse the comparison direction to change the sort direction to descending.
For the comparison, when you see a[i] < a[i] in pseudocode, that translates to:
a[i].compareTo(a[j]) < 0
The same goes for all other comparisons. Replace "< 0" with ">= 0" to compare a[i] >= a[j], and so on.
- Lucius T FowlerLv 74 years ago
The best book on sorting algorithms still is
https://www.amazon.com/Algorithms-Data-Structures-...
(not an affiliate link, buy it at the bookstore you trust)
If you haven't read that book, you know nothing about computer science.
My opinion. Others may vary.