• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList Sorting  RSS feed

 
Hend Hamad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an ARrayList of objects (MAC addr., RSSI), and it is sorted by MAC addr. I want to sort each group of MAC addr. by RSSI.

Example:
MAC 00:aa:bb:cc:dd:ee RSSI -90
MAC 00:aa:bb:cc:dd:ee RSSI -100
MAC 00:aa:bb:cc:dd:ee RSSI -600
MAC 00:aa:bb:cc:dd:ee RSSI -80

MAC 00:aa:bb:cc:dd:ff RSSI -100
MAC 00:aa:bb:cc:dd:ff RSSI -90


This is the sorted ArrayList, how can I sort group MAC 00:aa:bb:cc:dd:ee by RSSI?

P.S. Everytime I run the application, I get different number of MAC groups. There is no way I can anticipate how many groups I will get beforehand.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you happen to read about Comparable? That is what you want. Just try it out. If you face some problems then do let us know.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if implementing Comparable is not possible you can always use a custom Comparator.
 
Hend Hamad
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've already customized Comparable to sort in two different ways (byMAC, byRSSI). The problem is sorting each group of sorted MAC by RSSI.

Here is my idea for the next step:
1- After sorting by MAC, find the last index of the first MAC.
2- Partially sort the arraylist from (begining, indexOf -1).

Now I can work on the partially sorted part of the arraylist to get whatever information I need, then repeat the same procedure on the rest of the MAC groups.

Am I on the right way here?!
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Mac addr and RSSI are like seperate members in your class, then in your compareTo() method, you can compare first on mac addr. If that is equal then go for RSSI.

Something like
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!