Bit Confused About Sorting in App Connect

I have an array that I want to sort on the client side… and when you sort something then you sort by a specific field… but the sort() option in the menus doesn’t give me an option to specify what that is.

Am I missing something obvious…?

Bump.

I hate to do this believe me. It’s even banned in SO. But sometimes it’s necessary :smiley:

https://lmgtfy.com/?q=javascript+array+sort

1 Like

@teodor, I’m having this issue again with using the sort option in the menus.

Can you help me to specify what I am sorting by please?

And can I do a sort direction too?

Thanks!

What is the issue you are having?

Well as I showed in the previous screen shot, when I select Sort from the menu, there is no option to choose what I am sorting by…

Antony, that’s how array sort works. Even @JonL links is quite helpful :slight_smile:

Here it’s explained how sort and reverse work wit arrays: https://www.w3schools.com/js/js_array_sort.asp

I.e.

The sort() method sorts an array alphabetically:

and

The reverse() method reverses the elements in an array.

I’m sorry @teodor, but your response and Jon’s make no sense to me.

Wappler has a user interface which means I don’t have to read about Javascript.

I don’t understand why is there no option to say what I am sorting by.

Here is my use case:

I have an array of attendees, and I want to be able to sort it in either direction by the field id.

How do I do that in Wappler?

I am guessing I can forward sort it like this:

attendees_activity.data.attendees.where(`is_a_test`, in_test_mode.value, "==").where(`booking_status`, 101, "==").sort('id.toNumber()')

How would I reverse sort it?

And why was I not able to select the id field from the menus?

These are standard javascript methods, that’s why i pointed you to a link where you can learn how to sort an array (non-keyed array) and why is there no “column” to sort by…

If you sort your data source directly (a keyed array) you can see the property field where you can select what “column” to sort by:

so just append .sort('your_column_here') after the where and it will be fine.

Thanks @Teodor.

What syntax will I use to change the direction of the sort?

.sort('your_column_here').reverse()
1 Like

I see what you mean, seems like data picker sees the array using .where as a non-keyed array so that’s why it doesn’t offer the property option.
@George will check this.

Okay, I am getting it now.

There is an “Array” with just one element per array item, like:

1
2
3

and there is a “Keyed Array” with more than one element per item:

1,John,Smith,Male
2,Jane,Doe,Female

So I am talking about an Array when I should be talking about at “Keyed Array”.

Thank you for helping me to see that!

Is there an option in the menu system to sort a Keyed Array? I can’t see one in the menus!

Here is my situation below.

ka3

Yippee, we are on the same page! :slight_smile:

Thanks @teodor.