1

We have a homework about sorting ID, Arrival Time, CPU Burst Time, and Priority and I'm only having a problem on the sorting part which I could really use some help. Also, the value of the ID, AT, CBT, and Prio depends on the user input. I'm also using dual dimension Array to store the ID,AT,CBT, and P.

For example

ID: 98
AT: 50
CBT: 60
P(1 is the highest & 5 is the lowest): 1

ID: 99
AT: 55
CBT: 51
P: 1

if I want to arrange it based on AT, the one with the lower AT would be printed first along with it's ID and etc. The sequence would be from least to greatest AT.

and if I want to arrange it based on CBT, just like the AT, it would be printed from least to greatest, but if there if there are multiple CBT that are equal to each other, their AT would determine who would be printed first.

and if I arrange it based on priority, the prio with the value 1 would be printed first up to 5, but if there are multiple prio with same value, their CBT would determine who would be printed first, but if their CBT is the same too, it's up to AT to determine it.

I want to learn how to do it using java.

if(userchoice == '1'){
  //infos[a][1] is where the AT value stored
  if(infos[0][1] < [1][1] && infos[0][1] < infos[2][1] &&....infos[4][1]){
    for(int b = 0;b<4;b++){
      System.out.print(infos[0][]+"\t");
    }
  }
  System.out.println();
  if(infos[1][1] > infos[0][1] && infos[1][1] < infos[2][1] &&....infos[4][1]){
    for(int b = 0;b<4;b++){
      System.out.print(infos[1][b]+"\t");
    }
  }
}

these are the actual output based on my given example

Arranged using AT:
ID: 98
AT: 50
CBT: 60
P: 1

ID: 99
AT: 55
CBT: 51
P: 1


Arranged using CBT:
ID: 99
AT: 55
CBT: 51
P: 1

ID: 98
AT: 50
CBT: 60
P: 1


Arranged using P:
ID: 99
AT: 55
CBT: 51
P: 1

ID: 98
AT: 50
CBT: 60
P: 1
EyLmao
  • 11
  • 5
  • Do you have to implement your own sort algorithm or can use some build-in sorting method? Like `Collections.sort()`? – Amongalen Aug 26 '19 at 08:43
  • 1
    Possible duplicate of [Java Comparator class to sort arrays](https://stackoverflow.com/questions/5393254/java-comparator-class-to-sort-arrays) – AxelH Aug 26 '19 at 08:49
  • Use [`Arrays.sort(T[], Compartor)`](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)) or if you start using a specific class you can use [`Arrays.sort(Object[])`](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])) if your object implement `Comparable`. – AxelH Aug 26 '19 at 08:52
  • I haven't tried those, but will eventually try your recommendation. Thanks – EyLmao Aug 26 '19 at 11:56

1 Answers1

1

If you are using Java, firstly avoid use of multidimensional arrays and create objects. You have Collections framework to make these stuff easy.

Further more, read this http://www.javapractices.com/topic/TopicAction.do?Id=10 , or articles on same topic to understand customizing sort orders and more.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • Please don't provide an answer through an external link. When the link will die, your answer will die with it. As a remainder, see [answer] – AxelH Aug 26 '19 at 08:53
  • but wouldn't it make my code much easier to sort? since there are multiple values that I have to move depending on different conditions. – EyLmao Aug 26 '19 at 12:18