Reputation: 31
I'm not good with comparators but I need to learn it, I've done this much so far but I keep getting error at 'System.out.println(compy("January", "March", "October", "April"));'... please help, I would like an explanation with a fix. Thank You!
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Months {
public static void main (String args[]){
System.out.println(Comp("January","May"));
System.out.println(Comp("October", "May"));
System.out.println(Comp("August","August"));
System.out.println(Comp("January", "March"));
System.out.println(compy("January", "March", "October", "April"));
}
static int Comp(String s1, String s2){
while (true){
int r = 0;
int s = 0;
if(s1.equals("January")){
r=1;
}
if(s1.equals("February")){
r=2;
}
if(s1.equals("March")){
r=3;
}
if(s1.equals("April")){
r=4;
}
if(s1.equals("May")){
r=5;
}
if(s1.equals("June")){
r=6;
}
if(s1.equals("July")){
r=7;
}
if(s1.equals("August")){
r=8;
}
if(s1.equals("September")){
r=9;
}
if(s1.equals("October")){
r=10;
}
if(s1.equals("November")){
r=11;
}
if(s1.equals("December")){
r=12;
}
if(s2.equals("January")){
s=1;
}
if(s2.equals("February")){
s=2;
}
if(s2.equals("March")){
s=3;
}
if(s2.equals("April")){
s=4;
}
if(s2.equals("May")){
s=5;
}
if(s2.equals("June")){
s=6;
}
if(s2.equals("July")){
s=7;
}
if(s2.equals("August")){
s=8;
}
if(s2.equals("September")){
s=9;
}
if(s2.equals("October")){
s=10;
}
if(s2.equals("November")){
s=11;
}
if(s2.equals("December")){
s=12;
}
if(r<s){
return -1;
}
if(r>s){
return 1;
}
if(r==s){
return 0;
}
}
}
public class Comp implements Comparator {
public int compare(Object o1, Object o2){
return Comp((String)o1, (String)o2);
}
}
void sort(List l){
Comparator compy = new Comp();
Collections.sort(l, compy);
}
}
Upvotes: 1
Views: 309
Reputation: 432
Including the actual error would help.
It looks to me like the error is you are compy a variable in the sort method. from the main method.
Try something like
package com.example;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class HomeWork {
public static void main(String args[]) {
MonthComparable compy = new MonthComparable();
List<String> myList = Arrays.asList(new String[] {"May", "June", "April"});
Collections.sort(myList, compy);
System.out.println();
}
public static class MonthComparable implements Comparator<String> {
@Override
public int compare(String left, String right) {
Integer leftValue = monthValue(left);
Integer rightValue = monthValue(right);
return leftValue.compareTo(rightValue);
}
private static int monthValue(String left) {
// TODO Implement this
return 0;
}
}
}
Upvotes: 0
Reputation: 8640
Ravi Thapliyal gave you good solution, but if i can suggest something, change your comparator
public class Comp implements Comparator<String> {
private List<String> months = Arrays.asList("January" , "February",...);
@Override
public int compare(String s1, String s2) {
return months.indexOf(s1)-months.indexOf(s2);
}
}
its much shorter and more readable
Upvotes: 2
Reputation: 51711
This is how you can sort a list with your code.
List<String> list = new ArrayList<String>();
list.add("March");
list.add("January");
list.add("October");
list.add("April");
System.out.println("Before sort: " + list);
// prints: Before sort: [March, January, October, April]
new Months().sort(list); // sort
System.out.println("After sort: " + list);
// prints: After sort: [January, March, April, October]
Upvotes: 3