Hi there!
I recently have been working on an AGOL dashboard that displays and lists Annexation Ordinances for a municipality within our County, and have run into a bump in the road relating to sorting/displaying list elements alphabetically after trying to standardize naming parameters of these ordinance titles.
The script I have been using to standardize these titles can be found below:
var textArray = Split($datapoint.ordinance, " ");
var ord_name_end = 'Yorkville Ordinance ' + Pop(textArray);
return {
textColor: '',
backgroundColor: '',
separatorColor:'',
selectionColor: '',
selectionTextColor: '',
attributes: {
ord_name_end,
}
}
The original strings (under the field ‘ordinance’) were either saved as “Yorkville __" or "Yorkville Ordinance __”.
(“___” = ordinance number).
In order to only return the last part of the original string (the ordinance number itself), I used a .split() function to create and call the last item returned for each ordinance, then appended that ordinance number to the string “Yorkville Ordinance”.
Only issue is that when you try and use the regular sorting feature on list items settings, the original strings for this field are being sorted alphabetically and not these new string values.
So, I have been looking at a few different methods for potentially sorting/displaying these new strings alphabetically and have seen Sort() and OrderBy() functions being used. It seems like Sort() is the way to go as OrderBy() cannot be used on a direct array?
I have tried testing out the below code, but keep running into errors.
//turning ord name string into an array/splitting by each space (" ").
var ord_array = Split($datapoint.ordinance, " ");
//returning the last item to only get the ord number
var ord_num = Pop(ord_array);
//sorting ord number
function compare_ord(a,b){
if (a[ord_num]<b[ord_num])
return -1;
if (a[ord_num]>b[ord_num])
return 1;
return 0;
};
var sorted_ord_num = Sort(ord_num, compare_ord);
//appending ord number onto the string 'Yorkville Ordinance ' to get consistent naming across each ord record
var ord_full_name = 'Yorkville Ordinance ' + sorted_ord_num;
//display parameters
return {
textColor: '',
backgroundColor: '',
separatorColor:'',
selectionColor: '',
selectionTextColor: '',
attributes: {
ord_full_name,
}
}
I’d assume I am just misunderstanding the Sort() function parameters or am not setting the array up correctly to be able to use this comparative statement?
Any insight or help you may be able to provide on this would be much appreciated!
Best,
Katie