[pdm-devel] [PATCH datacenter-manager 06/11] ui: add helper to compare two strings with `localeCompare`

Dominik Csapak d.csapak at proxmox.com
Fri Sep 26 09:20:26 CEST 2025


sometimes it's useful to have a locale aware comparison, e.g. for
sorting, so abstract that away here.

It also has an option to sort numerically, which would sort, e.g.
'foo-101-bar'
'foo-1000-bar'

instead of in reverse

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 ui/src/lib.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/ui/src/lib.rs b/ui/src/lib.rs
index d8ba4b7b..a2b79b05 100644
--- a/ui/src/lib.rs
+++ b/ui/src/lib.rs
@@ -1,3 +1,4 @@
+use js_sys::{Array, JsString, Object};
 use pdm_api_types::remotes::RemoteType;
 use pdm_api_types::resource::{PveLxcResource, PveQemuResource, PveSdnResource};
 use pdm_client::types::Resource;
@@ -209,3 +210,21 @@ pub(crate) fn get_resource_node(resource: &Resource) -> Option<&str> {
         Resource::PbsDatastore(_) => None,
     }
 }
+
+/// Wrapper to 'locale compare' to strings
+///
+/// Note: The first parameter must be a [`String`], since it needs to be converted to a [`js_sys::JsString`].
+/// The `numeric` parameter corresponds to the numeric parameter of `String.localeCompare` from
+/// Javascript.
+///
+/// Seel also
+/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
+pub(crate) fn locale_compare(first: String, second: &str, numeric: bool) -> std::cmp::Ordering {
+    let first: JsString = first.into();
+    let options = Object::new();
+    // TODO: find a better way to create the options object
+    let _ = js_sys::Reflect::set(&options, &"numeric".into(), &numeric.into());
+    first
+        .locale_compare(second, &Array::new(), &options)
+        .cmp(&0)
+}
-- 
2.47.3





More information about the pdm-devel mailing list