Sort arraylist of object of class

I cant understand how to sort an arraylist of class instance by specific value of objects. Please help @amanagar, @Vinod_Crio_TA, @Pallav_crio, @anand-crio

Hi @Soumyadip,

Some of the possible methods are:

  1. Use a sorting algorithm like insertion sort by accessing a particular attribute of an object like age as the comparison value

  2. Making use of the Comparable interface which helps to sort our ArrayList using the Collections.sort() method if our class implements the interface

  3. Using Comparator interface similar to Comparable. The difference being that using Comparable we can only define a single comparison logic like either by age or by salary but not both. Using Comparator, we can have multiple methods each implementing different comparison logic.

See this article for how to implement 2 & 3 and their differences

  1. Using Comparator.comparing() method, we don’t need to make any changes to our classes. We can include a single line of code to sort. See here

Hope it helps

1 Like

@nabhanpv Thanks bro that problem is resolved. But I have one doubt. I cant understand that my extracting opening and closing value is right or not. As I calculated the of getAnnualizedReturn manually, but that value is not same that is required to pass the mainCalculateAnnualReturn() in test file. And I use the specified date in testfile. What to do?

We need to get the opening value of stock for the purchase date whereas for the end date, we use the stock’s closing value

If you’re getting somewhere close to the original value, like getting 0.8 instead of 0.84, it could have to do with the precision level

@nabhanpv by getOpen() and getClose I took that value. Am I right?
I got value far from expected like .022

You could go through the lines of code where the calculation is performed by debugging to see where the mistake could be happening. After setting breakpoints on a line of code let’s say

a = (b x c) + (d - a)

we can see what any part of this expression evaluates to by highlighting part of this expression let’s say b x c and then selecting Debug evaluate from the drop-down on right-clicking. This was shown in the CrioBytes session by @anand-crio. You can cross-check this output with the value you manually calculate.

@nabhanpv There is any method to order reversely using comparator??

Yes, just reverse the comparison logic. If you are returning 1 when object1.age > object2.age and -1 when object1.age < object2.age change this appropriately.

A much better way is using an additional argument to Collections.sort() instead of changing your comparison logic. You can easily find this by google search.

@nabhanpv Thanks sorting problem is completely sort

@nabhanpv Manual value and given value on cmd from my code both are same, but that doesn’t match expected one. I cant understand am i fetching correct value or not?
I got opening price for three symbol as
154.89 99.55 1027.2

and closing price like
157.92 101.12 1054.68

end date 2019-12-12
and getAnnualizedReturn()
0.020768796948932966 0.01674174787252869 0.02840846034866029

@Soumyadip
You are making a subtle error. I’ll tell you how I got to find it instead of telling the reason. That way, you’ll get to catch your very own bug, youselves :slight_smile:

Use Postman to get the stocks for any of the symbols(eg: AAPL) from its start date(in trades.json) to the end date the test uses as it’s argument. Can you find out where your values 154.89 & 157.92 occurs?

Ask yourselves this: Am I taking the stock open value for the start date and stock close value for the end date? Prove to yourselves with the result from Postman