From 0628fc1146da4ecd72d8bb32e025fecf227a41a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Mon, 8 Nov 2021 16:19:08 +0100 Subject: [PATCH] MOBILE-3833 sites: Improve site sorting --- src/core/services/sites.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index abbcc0de6..116f125dd 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -1090,7 +1090,7 @@ export class CoreSitesProvider { } /** - * Get the list of sites stored, sorted by URL and full name. + * Get the list of sites stored, sorted by sitename, URL and fullname. * * @param ids IDs of the sites to get. If not defined, return all sites. * @return Promise resolved when the sites are retrieved. @@ -1098,24 +1098,28 @@ export class CoreSitesProvider { async getSortedSites(ids?: string[]): Promise { const sites = await this.getSites(ids); - // Sort sites by url and fullname. + // Sort sites by site name, url and then fullname. sites.sort((a, b) => { - // First compare by site url without the protocol. - const compare = a.siteUrlWithoutProtocol.localeCompare(b.siteUrlWithoutProtocol); + // First compare by site name. + let textA = CoreTextUtils.cleanTags(a.siteName).toLowerCase().trim(); + let textB = CoreTextUtils.cleanTags(b.siteName).toLowerCase().trim(); + let compare = textA.localeCompare(textB); if (compare !== 0) { return compare; } - // If site url is the same, use fullname instead. - const fullNameA = a.fullName?.toLowerCase().trim(); - const fullNameB = b.fullName?.toLowerCase().trim(); - - if (!fullNameA || !fullNameB) { - return 0; + // If site name is the same, use site url without the protocol. + compare = a.siteUrlWithoutProtocol.localeCompare(b.siteUrlWithoutProtocol); + if (compare !== 0) { + return compare; } - return fullNameA.localeCompare(fullNameB); + // Finally use fullname. + textA = a.fullName?.toLowerCase().trim() || ''; + textB = b.fullName?.toLowerCase().trim() || ''; + + return textA.localeCompare(textB); }); return sites;