Does HasNameAndAge means Person class in my example?Stephan van Hulst wrote:
Ganesh Patekar wrote:It works properly. Is this correct?
Liutauras Vilda wrote:
Ganesh Patekar wrote:It works properly. Is this correct?
There is a one side effect. Your original array personList gets altered after the sortByXXX gets invoked. That is not always correct, in your case I'd say incorrect as there are no records that you were aware of that. In case otherwise, since original array gets altered, you don't even need to return anything from the sort method. Check if that was your intention.
JLS 10.7.1 wrote: Arrays Are Cloneable (Only one dimensional (In multidimensional subarrays are shared))
Stephan van Hulst wrote:You're using a form of selection sort. Selection sort is an inefficient sorting method.
Stephan van Hulst wrote:You duplicated your sorting code. Code duplication is bad.
In my opinion there isn't needed to write PersonSorter, especially if it sorts by name. As was mentioned earlier, if you'd sort Dog's array by their name, you'd end up implementing DogsSorter class, if got cows, you'd need to implement CowsSorter which basically would look similarly except sortByName method's parameter type.Ganesh Patekar wrote:
Ganesh Patekar wrote:I apologize for being late, my internet connection was interrupted since last night
I created comparator for name and age and used with Arrays's sort method.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:and if you're on version 8, you can use the new API to sort on any combination of "indices" you like, eg:
Arrays.sort(people, Person.Index.NAME.thenComparing(
Person.Index.AGE.reverse());
Ganesh Patekar wrote:
Ganesh Patekar wrote:
Ganesh Patekar wrote:Should I invoke this method where throw new statement is(I mean replacing throw new)? in above BUBBLE_SORT element of enum? I'll put this BubbleSorter in package algorithms.
Yes I meant the same but didn't write by natural order of date of birth just to express common meaning of that.Stephan van Hulst wrote:These comments are incorrect. These comparators don't compare by natural order. They compare by natural order of dates of birth.
Ohh! I was wondering why we had default method in that interface.Stephan van Hulst wrote:You don't need to use Period.between(). HasAge already has a method you can use: getAgeAt().
Ehh, didn't notice, yes It's T[] not int[]. Anyway I will try all these stuff later since I need to pay attention on basic core, once I finish those will definitely come to this again for sure. Because all these generic concepts are really interesting.Stephan van Hulst wrote:Yes, you should replace the throw statement. However, you can't just call your sorting algorithm like that, because it sorts int arrays, and you want to sort T arrays.
Ganesh Patekar wrote:That is stream API of Java 8 correct?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Oh I see, yes I'm yet to study algorithms (Had done before but long time ago so need to revise) and their performance in different situations so I can understand which algorithm is good to use in which situation. Also about different list frameworks performance in different operations like search, delete etc. Thank you for Cycle sort linkWinston Gutkowski wrote:No, but the new Comparator API uses functional interfaces, which are a parallel development. In fact, Comparator itself IS a functional interface now.
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|