Android Question Sorting List of numbers of Long Type

shashkiranr

Active Member
Licensed User
Longtime User
Hi,

I have a (Favourite_List)List containing Number of Long Type. If i use the command

B4X:
Favourite_List.Sort(True)
I get the ClasscastException

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at java.lang.Long.compareTo(Long.java:32)
at java.util.ComparableTimSort.binarySort(ComparableTimSort.java:228)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:154)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:142)
at java.util.Arrays.sort(Arrays.java:1973)
at java.util.Collections.sort(Collections.java:1867)
at anywheresoftware.b4a.objects.collections.List.Sort(List.java:144)
at com.bayalu.KannadaSankalana.main._ulv_main_load(main.java:3898)
at com.bayalu.KannadaSankalana.main._get_selected_screen(main.java:1680)
at com.bayalu.KannadaSankalana.main._ulv_sidebar_itemclick(main.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA$2.run(BA.java:285)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5155)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)

Is there any way to convert Long Type to Int type or vice versa?

Kindly let me know

Regards,
SK
 

Straker

Active Member
Licensed User
Longtime User
I think you got an error because, for some reason, some entry in your list are strings.
'Sort' can work only with items of the same type.
Can you post the code which fills the list?
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Here is the code

B4X:
Sub Select_Items(ItemID As Long) As Boolean
        Log("itemid - "&ItemID)
        Dim exist_selected_item As Boolean
        For i = 0 To Selected_Items.Size-1
            If Selected_Items.Get(i) = ItemID Then
                exist_selected_item = True
                Exit
            Else
                exist_selected_item = False
            End If
               
        Next
           
        If  exist_selected_item Then
            For i = 0 To Selected_Items.Size-1
                If Selected_Items.Get(i) = ItemID Then
                    Log("item removed "&Selected_Items.Get(i))
                    Selected_Items.RemoveAt(i)
                    Exit
                End If
            Next
        Else
            Selected_Items.Add(ItemID)
        End If
       
        Return exist_selected_item       
End Sub
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
First is better if you clean the code.
You can check if an item is already in list with

B4X:
      Dim idxExist as int
       IdxExist = Selected_Item.IndexOf(ItemID)
      If IdxExist  <> -1 then
           Selected_Item.RemoveAt(idxExist)
           Return true
       Else
             Selected_Item.add(ItemID)
       End If

When do you perform the 'sort' in your code?
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Hi Starker,

Thank you for kind reply. Actually the list will contain numbers of Long Data Format. All i want to do is sort it in ascending order. So when i use

B4X:
Favourite_List.Sort(True)
i get the exception mentioned in the first post. I want to perform the Sort after all the ItemID(1 to 50) is added to the list.

Regards,
SK
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Ok, you have found the solution (sorting methods), but as Erel has posted, there is a string between your long. Therefore your code is not 'strong' and you should better check where this string enters in your list...
 
Upvote 0

shashkiranr

Active Member
Licensed User
Longtime User
Hi Starker.

I have rechecked it many times. Infact to use the sort method everytime, i have to copy the contents of the list to an array of int and the sort the array and recopy the sorted array back into the list. So there is no string present.

Regards,
SK
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
Ok, but the error you posted is very clear. It says that for the compiler is impossible to transform (cast) a string into a long

B4X:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
 
Upvote 0
Top