Hi
I've just found this good piece of code that I want to apply in sorting my lists of maps?
By Erel from here
I have tweaked it so that I can do the same for a list of maps.
The compiled code
Its raising an error on this line
Error
Thank you in advance.
I've just found this good piece of code that I want to apply in sorting my lists of maps?
B4X:
Type SortHelper(SortKey As String, Value As Object) 'add in Process_Globals
Sub SortListOfArrays(Table As List, Index As Int) As List
Dim temp As List
temp.Initialize
For Each row() As String In Table
Dim sh As SortHelper
sh.SortKey = row(Index)
sh.Value = row
temp.Add(sh)
Next
temp.SortType("SortKey", True)
Table.Clear
For Each sh As SortHelper In temp
Table.Add(sh.Value)
Next
Return Table
End Sub
By Erel from here
Sorting algorithms - Teaching with Basic4android
Implementations of sorting algorithms are useful for teaching fundamental concepts such as algorithms analysis, complexity and data structures. The following code includes implementations of the following sorting algorithms: bubble sort, selection sort, quick sort, merge sort and binary tree...
www.b4x.com
I have tweaked it so that I can do the same for a list of maps.
B4X:
Sub ListOfMapsSort(lst As List, orderBy As String) As List
Dim fields As List
fields.Initialize
For Each fldm As Map In lst
Dim sname As String = fldm.Get(orderBy)
Dim sh As SortHelper
sh.Initialize
sh.SortKey = sname
sh.Value = fldm
fields.Add(sh)
Next
fields.SortType("SortKey", True)
Dim sorted As List
sorted.Initialize
For Each sh As SortHelper In fields
sorted.Add(sh.Value)
Next
Return sorted
End Sub
The compiled code
B4X:
// [4768] Sub ListOfMapsSort(lst As List, orderBy As String) As List
this.listofmapssort=function(_lst,_orderby) {
if (_B==null) _B=this;
var _fields,_fldm,_sname,_sh,_sorted;
// [4769] Dim fields As List
_fields=[];
// [4770] fields.Initialize
_fields.length=0;
// [4771] For Each fldm As Map In lst
for (var _fldmindex=0;_fldmindex<_lst.length;_fldmindex++) {
_fldm=_lst[_fldmindex];
// [4772] Dim sname As String = fldm.Get(orderBy)
_sname=_fldm[_orderby];
// [4773] Dim sh As SortHelper
_sh=(typeof banano_sithasodaisy_sorthelper === 'function') ? new banano_sithasodaisy_sorthelper(): null;
// [4774] sh.Initialize
_sh.initialize();
// [4775] sh.SortKey = sname
_sh._sortkey=_sname;
// [4776] sh.Value = fldm
_sh._value=_fldm;
// [4777] fields.Add(sh)
_fields.push(_sh);
// [4778] Next
}
// [4779] fields.SortType( {2219} , True)
if (!isNaN(parseFloat(_fields[_fields.length-1]["_SortKey"])) && isFinite(_fields[_fields.length-1]["_SortKey"])) {
_fields.sort(function(a, b){return a["_SortKey"] - b["_SortKey"]});
} else {
_fields.sort(function (a, b) {return a["_SortKey"].localeCompare(b["_SortKey"]);});
};
// [4780] Dim sorted As List
_sorted=[];
// [4781] sorted.Initialize
_sorted.length=0;
// [4782] For Each sh As SortHelper In fields
for (var _shindex=0;_shindex<_fields.length;_shindex++) {
_sh=_fields[_shindex];
// [4783] sorted.Add(sh.Value)
_sorted.push(_sh._value);
// [4784] Next
}
// [4785] Return sorted
return _sorted;
// End Sub
};
Its raising an error on this line
B4X:
_fields.sort(function (a, b) {return a["_SortKey"].localeCompare(b["_SortKey"]);});
Error
B4X:
TypeError: Cannot read properties of undefined (reading 'localeCompare')
Thank you in advance.