Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Asciibetically

 
Lexy Yang
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you sort strings asciibetically?
 
Pondurai Singh
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example program Sort an array of ASCII strings into ascending order
Reference: oreilly.com/9781565923713/
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense, but I would never use anything from that code:
1) That Comparer interface is exactly the same as the old non-generic java.util.Comparator interface.
2) That Comparable interface is exactly the same as the old non-generic java.lang.Comparable interface.
3) That sorting functionality already exists in java.util.Arrays. To sort in reverse order, you can use one of the two reverseOrder methods in java.util.Collections.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . To sort in reverse order, you can use one of the two reverseOrder methods in java.util.Collections.
Or you write a ComparatorNow that is really complicated, isn’t it
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That compare method is 100% identical to the implementation of reverseOrder(). That's also a singleton which makes it slightly more efficient - no matter how many times you call the method, only one object is created.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hadn’t noticed that reverseOrder creates a Comparator rather than reversing the list
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic