Repeater not sorting alphabetically with Dataset

Hello, I have a page with a “dataset1” (it has a sort of Category A-Z), the repeater is called"repeater2" and database called “Categories”( it is sorted by category alphabetically). The field I am trying to display has a key name of “category”. I have tried many approaches and have read all of the articles I could find on the subject.

The code is as follows:
import { local } from ‘wix-storage’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady(() => {
});
export function dataset1_ready_1() {
wixData.query(“Categories”)
.limit(300)
.find()
.then(results => {
$w(“#repeater2”).data = Array. from (results.items.reduceRight((m, t) => m.set(t.category, t), new Map()).values());
})
}

The result are as follows: You will notice it is sorted until it reaches the 25th display on ???

It;s hard to understand what you’re trying to do. You have a dataset but you don’t use it, and instead you pull the data directly from the collection and then you process it in a complicated way that is not clear to outsiders.
Anyway, if you want the data to be sorted alphabetically, then add to your query:

//...code
 wixData.query("Categories")
.ascending("category")
.limit(300) 
.find()
//code....

J.D. Thank you for the quick response. I will try your suggestion.

Let me explain further. I am eliminating duplication of Categories with the code . I have Categories with many subcategories in the database.

For example: a Category is Bathroom has the Subcategories of Bathroom design, Bathroom installation, Electrical, Plumbing, Cabinets, Tiling, Sinks, Toilets and Bathtubs. One Category with 9 Sub Categories. I then want to display the repeater Categories alphabetically.

You can see the page on my live site at: https://www.donepronto.com/search-results.

I hope this clarifies things further.

Kindest Regards,
Bill

J.D. THANK YOU…SOLVED:
Added 1) .ascending(“category”) and 2) .reverse()
New Code that works:

export function dataset1_ready_1() {
wixData.query(“Categories”)
.ascending(“category”)
.limit(300)
.find()
.then(results => {
$w(“#repeater2”).data = Array. from (results.items.reduceRight((m, t) => m.set(t.category, t), new Map()).values()).reverse();
})
}

I see. It’s a really complicated way to omit duplicates. There’re simpler ways.
But if it works for you then all is good.

J.D. If you have a easier way I am interested. I am always trying to improve my coding and I love WIX.
Kindest Regards,
Bill

OK. So, for example:

//code... query
.then(res => {
    let items = res.items;
    let categories = items.filter((o, index) => items.findIndex(e =>     e.category === o.category) === index);//"category" here is the field key of the parent category in your collection
    $w("#repeater2").data = categories;
})

J.D. Thank you Again.

I will try this code and let you know if I am successful. Your code is so much easier to understand and I appreciate that.

Kindest Regards,
Bill

Also you may consider using the query alone or the dataset alone. there’s no reason to go to the back end twice.

Thank you J.D. Coding and testing now. Bill

Hi J.D. tried code below and got the wrong results, everything is duplicate on display?:

export function dataset1_ready_1() {
wixData.query(“Categories”)
.ascending(“category”)
.limit(300)
.find()
.then(res => {
let items =res.tems;
let categories = items.filter((o, index) => items.findIndex(e => e.category === o.category) === index);
$w(“#repeater2”).data = categories;
})

Back to first solution. Thank you for your help. Bill

OK, I don’t see any problem wit the code you posted (except for the lack of closing } at the end of the function which you probably just didn’t copied to here).
Anyway as long as your first code works, it’s alright.

@jonatandor35 Thank you for checking.I will check again this afternoon and let you know. Bill