• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to order words different only in the case?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what will the following program print?
specify the words in order they will be printed.

import java.util.arrays;
import java.util.collections;
import.java.util.list;
public class test class
{
public static void main(String[]args)
{
String[]names=new String[]{"bb23", "BB23", "23bB", "b23B", "bB23", "B23b", "23Bd");

list <String> list=Array.aslist(names);
Collections.sort(list);
System.out.println(list);
}
}

my question is how java does order words difference only in the case?
like these above: "bb23", "BB23", "23bB", "b23B", "bB23", "B23b", "23Bd

please somebody out there help me!
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you perform a comparison between objects such as when you call Collections.sort(), the compareTo() method on the objects being sorted is invoked. In this example String objects are being sorted. Luckily for us, the String class has over-ridden the equals() and compareTo() methods to perform string comparisons. This is why the Strings are being printed in that particular sequence.

digits < uppercase < lowercase
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check the API documentation for the sort(List) method in Collections, you will see that "All elements in the list must implement the Comparable interface." In your example, the elements are Strings, so if you check the compareTo method of String, you will see exactly how this works.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic