How to sort dataset by data present in array of objects?

Hi! I have have a dataset that is connected to a collection which contains a number of fields. Some of those field are arrays of objects. The following snippet shows the “members” field of my collection for one specific row of my collection. Each row contains a similar array of objects.

[
  {
    "Scores": {
      "Tastes": {
        "characteristics": {
          "colors": [
            0
          ]
        }
      },
      "bodyType": -3,
      "brand": -3,
      "ItemType": 0,
      "seasonalType": -3
    },
    "Match": 70,
    "userId": 1
  },
  {
    "Scores": {
      "Tastes": {
        "characteristics": {
          "colors": [
            0
          ]
        }
      },
      "bodyType": -3,
      "brand": -3,
      "ItemType": 0,
      "seasonalType": -3
    },
    "Match": 80,
    "userId": 2
  },{
    "Scores": {
      "Tastes": {
        "characteristics": {
          "colors": [
            0
          ]
        }
      },
      "bodyType": -3,
      "brand": -3,
      "ItemType": 0,
      "seasonalType": -3
    },
    "Match": 75,
    "userId": 3
  },{
  ...
  }
]

I also have a repeater that is connected to this dataset. In case site member X with a certain userId visits my page, I want to display those repeater items with the best match first. Imagine for a second that member X has userId equal to 1. This means I need to look at members[0] for each row in the collection to get the match, hence members[0].Match.

Now I would like to tell my repeater to display its items in a descending order, starting from the item that has the largest Match. For that, I’d have to sort all the items in my collection based on the values that are stored in members[0].Match on each row. This would result in something like:

function sort() {
  var newSort = wixData.sort().descending("members[0].Match");
  console.log(newSort);
  $w("#dataset1").setSort(newSort);
}

Unfortunately I can only work with members.Match and not with specific indices. Is there a workaround? @J. D. we talked about this before, so maybe you can shed some light on this? See: https://www.wix.com/velo/forum/community-discussion/wixdata-filter-for-data-field-containing-array-of-objects

Thanks!

Try:

.ascending("members.Match")

@jonatandor35 that only makes sense when filtering, which is the whole point - when filtering you add a specific condition which allows you to select one particular index from your array of objects. When sorting, however, I really need to be able to pass the index as there is no such thing as a condition. Also, there might be more than 2 userIds, so switching between ascending/descending wouldn’t help either.

@vervoortyves I think I don’t really understand what you’re ting to do.
Maybe you can elaborate/clarify with examples.

@jonatandor35 I’ve updated my question. Is it clear now? If not, let me know!

@vervoortyves In that case I wouldn’t connect the dateset to the repeater via the editor, but do everything with code.
first I’d get an array of all the items - dataset .getItems(0,1000)
Then I’d sort them using JavaScript .sort() method.
Bind the sorted data to the repeater using repeater.data
And use repeater .onItemReady() to connect the fields to the repeater elements.

@jonatandor35 Sounds to be a logical way of doing.

@russian-dima
Yes, I think that in complicated cases, using JS on the results is much easier,
Sometimes it’s the only way (for example I had to sort by absolute value: 0, 1, 2, -2, -3, 4. And that’s not supported by the query API, but it’s easy to do it using JS on the results etc.) .

Thanks @jonatandor35 ! Makes sense. Thanks for confirming @russian-dima .