Sorting an Array with an Index in the Xano (@nocodebackend) Function Stack

Sort a data array using a separate index array in Xano. Loop through the index values, use find first element to match each one against your data, and append matches to a new array. The result skips any index values that don't exist in your data.
Chapters
So let's say that you have two arrays. And we'll call those two arrays data. And another one that I'm just going to call index, which is going to be just a list of integers. And the idea we have is that we want to basically sort data based on the order of stuff that's in the second array, which is index. So if I were to have— I don't need to do that. Let's just say we're going to run debug here, just what the source data would look like. So if data is an array, and that array contains, you know, say, 3 objects. And each object contains a key, or we'll call it ID, because this actually is— well, we'll call it key, of, you know, 1, comma, and we'll just put something else in just so it's easy to keep track of which is which, a value of x. And we'll just copy that for a second. Copy, paste, and we'll say it's a key of 2 and a value of y. And then I have a key of, you know, 17, right? Remember, these don't need to be in order, be sequence or anything, and a value of w. So I have, you know, 1x, 2y, 17w. And now let's say I further have an index of, let's say, you know, 17, 5, 1, 4, 2, right? So let's— and of course, I only have 3 of those are actually sitting inside my array of data. But the index is telling me what order things should be appearing in. That could be kind of, you know, messy in terms of this relationship. My objective is I would like to have data be sorted by their positions in index. How do you do that? Well, of course, right now it doesn't do anything. I was just doing that to save the input data. But what we really want to do is we're going to use the index to create the order in which we're going to do things. And then we're going to iterate over those in the, in the data, and then we're going to build out the newly sorted data. So I'm going to create a new variable and call it sorted data. And it's just going to be a blank array. It doesn't have anything in it. This is a very common, what we call reducer pattern. And then after that, I'm going to say, let's also have a, we're going to make a loop, right? We're going to say we're going to loop over, do, do, do, do, do. And we're going to say, not database requests, data manipulation, great. And we're going to do loops, and we're going to loop over our index as item. Okay. So the idea now is we're just gonna be looping over each item in the index. And the reason why we're looping over the index and not the data is because the sequence of the index, right, that 17 comes before 5 comes before 1, is what matters for the purpose of the sort that we're creating here. And then we're gonna do a foreach loop. Well, inside the forEach loop, we're going to, we're going to do data manipulation. We're going to really, we want to get the variable from data that matches this key. And the way that we are going to do that is going to be through searching through data to go find the element that matches that key. And we do that through an array function. Called find first element. And we're going to be searching through data for the element that basically has a key that matches the current index value. And so I'm looking for where this, which is the iterator variable, dot key is equal to my current item, which is the current item I have in the index. So the first one's gonna be 17, then 5, then 1, et cetera. And I'm going to say okay to that. And I'm going to found, you know, say found item. Now we now need to ask, was the thing from the index, did we find anything, right? And so we're going to say data manipulation, we're going to use conditional. We're going to say if found item is not equal to null. Right, I found an object, didn't just find nothing. Great. So if it found something, then we go back to manipulation, we go to arrays. And we say add to the end of the array, we want to add to the end of sorted data, whatever it was that we just found. And really, that's it. And then we'll just return sorted data at the end. Okay, so what we've done here, we have said that we have these, we've got our main data, and we have this index that we're going to iterate over. And what our expectation is that we should, you know, first we'll look at 17. And we'll say, is there anything in data that has a key of 17? And the fact there will be, and you'll see that will match with W, and then we'll add that to the end of sorted_data, which is of course then blank, so that will become the first item. And then it'll look at 5 and it will say, well, is there anything in it? Is there anything in there that matches a 5? And the answer will be no, it will be null. And it will therefore not add it to the end. Does anything match a 1? Yes, that will be the X. Anything that matches a 3? No. Anything that matches a 2? And the answer will be yes, will be a Y. So my expectation is this should generate a W. X, y order, based on this array of index and this array of data. Let's see if that works. w, x, y, skipping over the things that aren't inside the index. It worked perfectly. Now the, the key ideas in here are that this is really a, this is a reducer function really, because we're reducing the index, right? Which is your list of numbers into a filtered version of the of the data array. And this idea of sort of combining these two different arrays is a little bit complicated conceptually, but super powerful. And the tooling in Xano allows it to be something you don't have to do with a whole lot of work. You see, we just did it with a few lines here. Key ideas here are going to be this find first element, which is one of the superpowers in Xano, being able to extract things from your arrays. I think the new expression builder might also give some alternative ways of doing that. We can cover that in a future video. But the idea of we're going to take the, take one array and we're going to sort it by another array, which is really data and index, is actually also paralleling the way that database tables work under the covers, where they keep separately this idea of the original data that you had in your, you know, all those fields are sitting inside a table, and an index, which is going to be the way it can be sorted for different use cases. And so basically, you've now re-implemented some of how a relational database works, just by adding these, you know, 4 lines into your, your Xano. Hopefully that helps. If you've got questions, please put it in the comments, and we'll see you next time.










