d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Multiple Return Statements > And Comparator
Add Reply New Topic New Poll
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 12 2015 12:22pm
I will preface this with the follow: the method header was provided and i have to build around it for the assignment...i think i wouldve tackled this in a different way...but thats how school works i suppose :)

The point of this program is to return True if the array is sorted in ascending fashion, and False if descending/unsorted.

Code:
Code

public class Part1 {
public static void main(String[] args) {
Comparator<? super Object> comp = new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return 0;
}
};
boolean ascending;
Integer[] array0 = {1, 2, 3, 4, 5};
System.out.println(ordered(array0, comp, true)); //Should print true

Integer[] array = {1, 2, 3, 4, 5, 3};
ascending = ordered(array, comp, true);
System.out.println(ascending); //Should print false

Integer[] array1 = {5, 4, 3, 2, 1};
System.out.println(ordered(array1, comp, true)); //Should print false

Integer[] array2 = {1, 5, 2, 4, 3};
System.out.println(ordered(array2, comp, false)); //Should print false

String[] array3 = {"a", "A", "c", "b"};
System.out.println(ordered(array3, comp, false)); //Should print false
}

public static<E>boolean ordered(E[] list, Comparator<? super E>comp, boolean ascending) {
if(ascending){
for (int i = 0; i < list.length - 1; i++) {
if (comp.compare(list[i], list[i + 1]) > 0) {
return false;
}
}
}
else {
for (int i = 0; i < list.length - 1; i++) {
if (comp.compare(list[i], list[i + 1]) < 0) {
return true;
} else if (comp.compare(list[i], list[i + 1]) > 0) {
return false;
}
}
}
return true;
}

}


Im pretty sure im missing something with the comparator, but im still new to using it and i cannot quite figure that one out. I understand that compare works as follows: 0 if o1=o2, -1 if o1<o2, and 1 if o1>o2.

That beings me to the ordered method, i tried it a few different ways (using a variable and returning it and all return statements as seen above). Neither seem to work properly...and it could be that im doing my return statements wrong, or that my comparator is incomplete.

Thoughts?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 12 2015 12:31pm
Quote
I understand that compare works as follows: 0 if o1=o2, -1 if o1<o2, and 1 if o1>o2.


Quote
Neither seem to work properly...and it could be that im doing my return statements wrong, or that my comparator is incomplete.


For starters, did you read what your compare method does? i don't see your 0, -1, 1 logic anywhere in there. Comparator is an interface; it's up to you to make it work.

Code
@Override
public int compare(Object o1, Object o2) {
return 0;
}


This post was edited by carteblanche on Mar 12 2015 12:32pm
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 12 2015 12:36pm
Quote (carteblanche @ 12 Mar 2015 13:31)
For starters, did you read what your compare method does? i don't see your 0, -1, 1 logic anywhere in there. Comparator is an interface; it's up to you to make it work.

Code
@Override
public int compare(Object o1, Object o2) {
return 0;
}


comp.compare(list, list[i + 1]) > 0
comp.compare(list, list[i + 1]) < 0
comp.compare(list, list[i + 1]) > 0

three times?


it also wont let me call compareTo(), which threw me for a loop i suppose because the only examples i found use that method call

This post was edited by Wacko on Mar 12 2015 12:37pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 12 2015 12:41pm
Quote (Wacko @ Mar 12 2015 02:36pm)
comp.compare(list, list[i + 1]) > 0
comp.compare(list, list[i + 1]) < 0
comp.compare(list, list[i + 1]) > 0

three times?


it also wont let me call compareTo(), which threw me for a loop i suppose because the only examples i found use that method call


You're using the Comparator interface; compareTo is part of the Comparable interface. Two different interfaces. Comparators are used when a class doesn't inheritantly have a sort order; like Customers or Orders. maybe you want to sort by name or date or whatever. Comparable is used when a class does inheritantly have a sort order; like Integer

your comparator always returns 0, eg says all objects are always equal. So everywhere in your code that you check for compare(..) > 0 or compare(..) < 0 aren't going to get executed. you need to fix your compare method.

This post was edited by carteblanche on Mar 12 2015 12:43pm
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 12 2015 12:56pm
speaking specifically about integers for now:
return o1 < o2 ? -1 : o1 == o2 ? 0 : 1;

this seemed to work fine until i sent the array {1,5,2,4,3} through.

It compares 1 to 5, sees that 1 is indeed less than 5, then returns true. How can i force a check of the whole array?
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Mar 12 2015 01:26pm
it's just
Code
return o2-o1;


also change
Code
if(ascending){
for (int i = 0; i < list.length - 1; i++) {
if (comp.compare(list[i], list[i + 1]) > 0) {
return false;
}
}
}


to

Code
if(ascending){
for (int i = 0; i < list.length - 1; i++) {
if (comp.compare(list[i], list[i + 1]) < 0) {
return false;
}
}
}


This post was edited by labatymo on Mar 12 2015 01:27pm
Member
Posts: 9,805
Joined: Jul 8 2008
Gold: 9.00
Mar 12 2015 01:45pm
Quote (labatymo @ 12 Mar 2015 14:26)
it's just
Code
return o2-o1;


also change
Code
if(ascending){
for (int i = 0; i < list.length - 1; i++) {
if (comp.compare(list[i], list[i + 1]) > 0) {
return false;
}
}
}


to

Code
if(ascending){
for (int i = 0; i < list.length - 1; i++) {
if (comp.compare(list[i], list[i + 1]) < 0) {
return false;
}
}
}


that portion of code is > 0 because im already asserting that the array is ascending to its checking so see if thats true or not, so if it were <0 (ie o1 < o2) it would need to return true.

This post was edited by Wacko on Mar 12 2015 01:47pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll