diff options
author | A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> | 2024-09-27 15:56:54 +0200 |
---|---|---|
committer | A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> | 2024-10-18 08:47:05 +0200 |
commit | 79f654ced5525515091c99a8d23dccb9c2a09b35 (patch) | |
tree | 8f17f2ef6cf280af8c8df6039f123a1182a54169 /core/variant | |
parent | 04692d83cb8f61002f18ea1d954df8c558ee84f7 (diff) | |
download | redot-engine-79f654ced5525515091c99a8d23dccb9c2a09b35.tar.gz |
[Core] Fix sorting of `Dictionary` keys
`StringName` keys were sorted as `StringName` which is unstable.
Diffstat (limited to 'core/variant')
-rw-r--r-- | core/variant/variant.h | 13 | ||||
-rw-r--r-- | core/variant/variant_parser.cpp | 2 |
2 files changed, 14 insertions, 1 deletions
diff --git a/core/variant/variant.h b/core/variant/variant.h index c76b849abd..3b1924e8ea 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -854,6 +854,19 @@ struct StringLikeVariantComparator { static bool compare(const Variant &p_lhs, const Variant &p_rhs); }; +struct StringLikeVariantOrder { + static _ALWAYS_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { + if (p_lhs.is_string() && p_rhs.is_string()) { + return p_lhs.operator String() < p_rhs.operator String(); + } + return p_lhs < p_rhs; + } + + _ALWAYS_INLINE_ bool operator()(const Variant &p_lhs, const Variant &p_rhs) const { + return compare(p_lhs, p_rhs); + } +}; + Variant::ObjData &Variant::_get_obj() { return *reinterpret_cast<ObjData *>(&_data._mem[0]); } diff --git a/core/variant/variant_parser.cpp b/core/variant/variant_parser.cpp index f5f96456d3..f05b9cd83a 100644 --- a/core/variant/variant_parser.cpp +++ b/core/variant/variant_parser.cpp @@ -2245,7 +2245,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str } else { List<Variant> keys; dict.get_key_list(&keys); - keys.sort(); + keys.sort_custom<StringLikeVariantOrder>(); if (keys.is_empty()) { // Avoid unnecessary line break. |