« first day (6 days earlier)      last day (20 days later) » 

5:22 AM
@MadProgrammer I am here :)
 
bingo
 
hope you are doing fine
just wanted to let you first, that I got a phone interview from google so I am studying for that
 
Working hard, staying awake in meetings
Awesome! Good luck!
 
you forgot the major part which is making money :D
thank you
so my question is about comparable and comparator
 
Not sure about that "major" part :P
 
5:24 AM
hope you do :)
comparable is realted to natural ordering
and compartor is doing the job?
but It seems I have diffculty to understand how to apply the concept
can you please teach me pleae?
 
They're both the same thing, implemented differently
 
public static void SortNotComparable() {
List<NotComparable> list = new ArrayList<>();

int i = 0;
while (i < 10) {
list.add(new NotComparable(i));
i++;
}

Arrays.sort(list.toArray(), new Comparator<NotComparable>(){
@Override
public int compare(NotComparable o1, NotComparable o2) {
if (o1.i > o2.i) {
return 1;
} else if (o1.i < o2.i) {
return -1;
} else {
return 0;
}
}
});
}
 
Comparable simply states that a given object is comparable to another object of the same type object.compareTo(object) in, more or less, direct manner...
 
I know this but how the comparison is done
because based on some samples yuo can accend and decesend
 
A Comparator is a means by which two objects which, aren't necessarily comparable, can be compared or weighted.
 
5:27 AM
since I do not want to memorize anything I do not understand it how?
 
I do quite complex comparisons with Comparator (and Comparable to a lesser extent) which includes sub group ordering...
 
what do you mean by your last sentence?
 
Well, if you have a list of names, with first and last names, you can actually use Comparator to sort the list by last name first and each sub name group by first name, for instance, in a single call.
 
damn really?
 
For me, I tend to use Comparator over Comparable as it allows me to change the way the comparison can work without need to sub class the Comparable object. Which you use will also be determined by the the requirements of the API
 
5:31 AM
did you see my code sample?
 
Yes. You could actually use the distance that two objects are different to optimise the search. Something like a quick sort might use the distance information to determine the position of the two objects for instance
 
but my code has a issue
it complains the list is type object not comparable even thought the list has a type notcomprable?
 
You need to use Collections.sort(list, ...)
 
I swtich my list to Array by using toArray
so I think it should be fine am I right?
 
list.toArray is returning a Object[], which obviously doesn't match the requirements of the Comparator
 
5:37 AM
ohhhhhhhhhhhhhh
 
If you use list.toArray(new NotComparable[list.size()]) it will work...
 
is it possible to cast it?
whynot casting it?
 
Because it's still an Object[]. toArray() literally creates an Object array (Object[] objs = new Object[size()]` or some such, so you would end up with a RuntimeException
 
k sounds awesome
thank you
last request
do you have any good tutorial for comparator and comaprable ?
public int compare(NotComparable o1, NotComparable o2) {
if (o1.i > o2.i) {
return 1;
} else if (o1.i < o2.i) {
return -1;
} else {
return 0;
}
}
can you just explan to me how the comparison is done here?
 
It's the difference between < 0, == 0 and > 0
 
5:43 AM
I know that
but how it is goona get applied in the list
for example
3 2 8 7 0 1
 
Do you know hoow a bubble sort works?
 
ya
it is that?
u r kidding me :D
 
The sort is more sophisticated than that (it's type of quick sort I think), but the basic premise is the same, if the result of the compare is > 0, than o2 is moved right, if it's < 0, then o2 is moved left otherwise the remain where they are...
 
oh k this is what I want
as you said can we make it better by using better sort like quick sort?
 
	package test;

	import java.util.ArrayList;
	import java.util.Collections;
	import java.util.Comparator;
	import java.util.List;

	public class Test {

		public static void main(String[] args) {
			new Test();
		}

		public Test() {
			List<Name> names = new ArrayList<>(25);
			names.add(new Name("Tina", "Brown"));
			names.add(new Name("David", "David"));
			names.add(new Name("Diana", "Cooper"));
			names.add(new Name("Willie", "Perry"));
			names.add(new Name("Jose", "Flores"));
			names.add(new Name("Annie", "Flores"));
The internal sort is pretty good, but you could use the Comparator and Comparable interfaces to write your sorts...
Sorry, an example of a dual sort, sorting by last then first name...
 
5:50 AM
it make me confuse but I guess it is good
 
Sorry gotta run, good luck with the interview ;)
 
thank you for your time
talk to you later
 

« first day (6 days earlier)      last day (20 days later) »