Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is better to create a Class and implement Comparable method </p> <pre><code>public class TimeChamp implements Comparable&lt;TimeChamp&gt;{ private short time; private String champs; public short getTime() { return time; } public void setTime(short time) { this.time = time; } public String getChamps() { return champs; } public void setChamps(String champs) { this.champs = champs; } @Override public int compareTo(TimeChamp o) { if(getChamps().compareTo(o.getChamps())==0) { if(getTime()&lt;o.getTime()) return -1; else return 1; } else return getChamps().compareTo(o.getChamps()); } } </code></pre> <p>Then you can create the function which will sort in your class like this</p> <pre><code>public void getSort(TimeChamp[] timeChamp) { Arrays.sort(timeChamp); for(int i=0;i&lt;timeChamp.length;i++) { System.out.print(timeChamp[i].getTime()); System.out.print(" "); System.out.println(timeChamp[i].getChamps()); } } </code></pre> <p>EDIT : Writing a more descriptive answer which constructs the data sets and calls the sort method</p> <pre><code>import java.util.Arrays; public class MainExample { /** * @param args */ public static void main(String[] args) { //Construct data sets TimeChamp [] timeChampArray = new TimeChamp[3]; TimeChamp timeChamp = new TimeChamp(); timeChamp.setChamps("Boat"); timeChamp.setTime((short)2001); timeChampArray[0]=timeChamp; timeChamp= new TimeChamp(); timeChamp.setChamps("Banana"); timeChamp.setTime((short)2000); timeChampArray[1]=timeChamp; timeChamp= new TimeChamp(); timeChamp.setChamps("Banana"); timeChamp.setTime((short)2001); timeChampArray[2]=timeChamp; //Call the sort method new MainExample().getSort(timeChampArray); } //the sorting method public void getSort(TimeChamp[] timeChamp) { Arrays.sort(timeChamp); for(int i=0;i&lt;timeChamp.length;i++) { System.out.print(timeChamp[i].getTime()); System.out.print(" "); System.out.println(timeChamp[i].getChamps()); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload