Question:
Array of String sorted in reverse order?
champinder89
2009-03-27 12:08:49 UTC
I want to sort an array in reverser order
THe array in my program consists of 7 days of the week
String strs[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" , "Saturday", "Sunday" };


I want the output which reads Sunday, saturday, friday, thursday, wednesday, tuesday, monday

below is the scratch code plzzz feel free to edit thanks in advance!!!!



import java.util.*;

public class Problem5 {


public static void main(String args[]) {

String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" , "Saturday", "Sunday" };

System.out.print("Initial order: ");

for(String s : days)
System.out.print(s + " ");
System.out.println("\n");

Arrays.sort(days);

System.out.print("Sorted in reverse order: ");
for(String s : days)
System.out.print(s + " ");
System.out.println("\n");


}
}
Four answers:
femtorgon2
2009-03-27 12:36:30 UTC
Arrays.sort(days);

Won't really get you anywhere here.

The Arrays.Sort(Object) method doesn't know anything about days of the week. I believe it will give you an alphabetical sort (using the String.compareto(Object) method)

You don't really need to change the order of anything in the array to make this work. All you need to do it loop through it backwards.

Also, I'm not really familiar with any syntax like what your using for your loops in Java.

Such as:



String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" , "Saturday", "Sunday" };



System.out.print("Initial Order\n");



for (int x = 0; x<6=; x++)

System.out.print(days[x]+"\n");



System.out.print("Reverse Order\n");



for(int x = 6; x>=0; x--)

system.out.print(days[x]+"\n");
2016-04-07 12:20:08 UTC
Sure, just iterate through the list on insertion to find where the new song title will go. Add an addition entry to the end (if necessary) and move everything down one space. Then insert the new song title. Suppose you have 20 song titles in the array and the array is already dimensioned for 1000. Find where the song title goes, for example array element 15. Move 20 to 21, 19, to 20, etc. Then copy the new title into slot 15.
deonejuan
2009-03-27 13:02:23 UTC
Generics and the Collections do a lot of the work for you...



import java.util.Arrays;

import java.util.Collections;

import java.util.List;



public class ReverseArray {

public static void main( String[] args ) {

String[] days =

"Sunday,Monday,Tuesday,Wednesday, Thursday,Friday,Saturday".split(",");

List list = Arrays.asList(days);

Collections.reverse(list);

System.out.println(list.toString());

}

}
VS
2009-03-27 12:31:08 UTC
Here is the corrected code:





String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" , "Saturday", "Sunday" };



System.out.print("Initial order: ");



for(String s : days)

System.out.print(s + " ");

System.out.println("\n");



//Arrays.sort(days); // what is this for. Sort will order it by name.



System.out.print("Sorted in reverse order: ");

for (int i=days.length; i>0; i--)

System.out.print(days[i-1] + " ");

//for(String s : days)

//System.out.print(s + " ");

System.out.println("\n");


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...