Sorting by character length

I am new to wix and new to coding.

I need to sort my repeaters according to character length, is there a way to do this?

I tried to search the internet and this forum but could not find any results.

I came as far as this:

$w ( “#dataset1” ). setSort (

 wixData . sort ()  
 . ascending ( "title" . length ) 

 ); 

it is not sorting working well though and it is making the overall code take much longer to execute

Thanks

Is this your FULL-CODE ?

This is the full code without the order.

import wixData from ‘wix-data’ ;
$w . onReady ( function () {
//TODO: write your page related code here…
});
let debounceTimer ;
export function searchBar_keyPress ( event ) {
$w ( “#loadingGif” ). show ();

**if**  ( debounceTimer ) { 
    clearTimeout ( debounceTimer ); 
    debounceTimer  =  **undefined** ; 
} 
debounceTimer  =  setTimeout (() => { 
    $w ( "#dataset1" ). setFilter ( wixData . filter () 
    . contains ( "title" ,  $w ( '#searchBar' ). value ) 
    
            ) 

        . then (() => { 
            count (); 
        }) 
},  200 ); 

}

function count () {
let total = $w ( ‘#dataset1’ ). getTotalCount ();
if ( total > 1 ) {
$w ( ‘#textResults’ ). text = ${ total } results were found. ;
$w ( “#loadingGif” ). hide ();
$w ( ‘#textResults’ ). show ();
}
if ( total === 1 ) {
$w ( ‘#textResults’ ). text = ${ total } result was found. ;
$w ( “#loadingGif” ). hide ();
$w ( ‘#textResults’ ). show ();
}
if ( total === 0 ) {
$w ( ‘#textResults’ ). text = “No result found!” ;
$w ( “#loadingGif” ). hide ();
$w ( ‘#textResults’ ). show ();
}
}


what I am doing is basically a dictionary.
I have a list of words and users can input the word in the searchbox and the repeaters that contain the words are filtered out.

The problem is that if a user wants to search for “z” he has to scroll down.
But if I order by:
1) character length
2) alphabetical order

Than the issue would be resolved satisfactorily.

Thanks :slight_smile:

The sort-function offers just 2-different sort-methods…
https://www.wix.com/velo/reference/wix-data/wixdatasort

You will have to write your sort-function on your own.

Example…

["c", "a", "b"].sort(function(a, b) {
  return a.length - b.length || // sort by length, if equal then
         a.localeCompare(b);    // sort by dictionary order
});

At the moment writing code is something that I need to learn.

I can go around it by:
I am preparing my data in excel.
If I add a field with character length in excel I can sort it according to this field.

Thanks