Android Question Sorting List of Custom Objects?

he4a

Member
Licensed User
Longtime User
Hi folks,

I'm trying to sort a list of a custom type called "ScanResult". This list holds the responses i get from scanning my surroundings for wireless networks and gathering their signal strength, ssid etc...

The custom object i made is this:

ScanResult:

Sub Class_Globals

Public MAC As String
Public Channel As Int
Public RSSI As Int
Public SSID As String

End Sub

These Objects are sorted into a List like this:

Public Sub AddScanResult(MAC As String, Channel As Int, RSSI As Int, SSID As String)

Dim it As ScanResult

it.Initialize
it.MAC = MAC
it.Channel = Channel
it.RSSI = RSSI - 128
it.SSID = SSID
ScanList.Add(it)
Log("ScanList.Size is now: " & ScanList.Size)

End Sub

But when i try to sort that list after either MAC adress or SSID using any of the given subs:

Sort
SortCaseInsensitive
SortType
SortTypeCaseInsensitive

i always get an error:

-------------------------------------------------------------------------------------------
Error occurred on line: 839 (DeviceSettings)

java.lang.ClassCastException: de.hallererne.mstknwlanconfigurator.scanresult cannot be cast to java.lang.Comparable

at anywheresoftware.b4a.objects.collections.List$3.compare(List.java:1)

at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320)

at java.util.TimSort.sort(TimSort.java:185)

at java.util.Arrays.sort(Arrays.java:1998)

at java.util.Collections.sort(Collections.java:1900)

at anywheresoftware.b4a.objects.collections.List.SortCaseInsensitive(List.java:222)

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)

at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)

at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)

at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:897)

at anywheresoftware.b4a.keywords.Common.CallSubNew3(Common.java:847)

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)

at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)

at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)

at anywheresoftware.b4a.BA$2.run(BA.java:328)

at android.os.Handler.handleCallback(Handler.java:746)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5443)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

---------------------------------------------------------------------------------------------------

maybe it's because there are several items with the same SSID and MAC in that List? so it's not a unique value? right now its like this:


SSID1
SSID3
SSID2
SSID1
SSID1
SSID2
SSID4
SSID3
SSID1

i just want to show it like this:

SSID1
SSID1
SSID1
SSID1
SSID2
SSID2
SSID3
SSID3
SSID4

is it possible to sort the list like this or do i have to think of some other way to do it?

Thanks in advance, Rob
 

udg

Expert
Licensed User
Longtime User
Hi Rob, please have a look at list.SortType

In your case it could be something like:
B4X:
ScanList.SortType("SSID",true)

udg
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Instead of a class you should use the Type command.
Define it in Globals or ProcessGlobals
B4X:
Type ScanResult(MAC As String, Channel As Int, RSSI As Int, SSID As String)

Then use it in your class
B4X:
Public Sub AddScanResult(MAC As String, Channel As Int, RSSI As Int, SSID As String)

Dim it As ScanResult

it.Initialize
it.MAC = MAC
it.Channel = Channel
it.RSSI = RSSI - 128
it.SSID = SSID
ScanList.Add(it)
Log("ScanList.Size is now: " & ScanList.Size)

End Sub

https://www.b4x.com/android/forum/pages/results/?query=type
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Manfred,
you're right. I don't know how I could have read "Public Sub AddScanResult(MAC As String, Channel As Int, RSSI As Int, SSID As String)" for "Type ScaResult..." but that was exactly what I considered while replying.
It's incredible that I missed entirely what was written in the post and worked a response based on what was in my mind..eheh
Fortunately I turned off the PC soon after that and resumed the reading of a good book.

bye
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…