Skip to content

Commit f9e145a

Browse files
committed
Implement PartialOrd and Ord for Emoji
This allows emojis to be compared and sorted. The order is based on the Unicode code point order.
1 parent 021e5c6 commit f9e145a

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

RELEASES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# 📝 Release notes
22

3+
## 0.7.2
4+
5+
*Unreleased*
6+
7+
- [Implement `PartialOrd` and `Ord` for `Emoji`][todo]. This allows emojis to
8+
be compared and sorted. The order is based on the Unicode code point order.
9+
310
## 0.7.1
411

512
*July 31st, 2025*

src/lib.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,26 +459,43 @@ impl Emoji {
459459
impl cmp::PartialEq<Emoji> for Emoji {
460460
#[inline]
461461
fn eq(&self, other: &Emoji) -> bool {
462-
self.emoji == other.emoji
462+
self.emoji.eq(other.emoji)
463463
}
464464
}
465465

466466
impl cmp::PartialEq<str> for Emoji {
467467
#[inline]
468468
fn eq(&self, s: &str) -> bool {
469-
self.as_str() == s
469+
self.emoji.eq(s)
470470
}
471471
}
472472

473+
// TODO: needed?
473474
impl cmp::PartialEq<&str> for Emoji {
474475
#[inline]
475476
fn eq(&self, s: &&str) -> bool {
476-
self.as_str() == *s
477+
self.emoji.eq(*s)
477478
}
478479
}
479480

480481
impl cmp::Eq for Emoji {}
481482

483+
impl cmp::PartialOrd<Emoji> for Emoji {
484+
/// Compares two emojis based on their *Unicode* value.
485+
#[inline]
486+
fn partial_cmp(&self, other: &Emoji) -> Option<cmp::Ordering> {
487+
self.emoji.partial_cmp(other.emoji)
488+
}
489+
}
490+
491+
impl cmp::Ord for Emoji {
492+
/// Compares two emojis based on their *Unicode* value.
493+
#[inline]
494+
fn cmp(&self, other: &Emoji) -> cmp::Ordering {
495+
self.emoji.cmp(other.emoji)
496+
}
497+
}
498+
482499
impl hash::Hash for Emoji {
483500
#[inline]
484501
fn hash<H: hash::Hasher>(&self, state: &mut H) {

tests/smoke.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::cmp::Ordering;
2+
13
use emojis::{SkinTone, UnicodeVersion};
24

35
#[test]
@@ -29,8 +31,29 @@ fn unicode_version_partial_ord() {
2931
}
3032

3133
#[test]
32-
fn emoji_partial_eq_str() {
33-
assert_eq!(emojis::get("😀").unwrap(), "😀");
34+
fn emoji_eq() {
35+
let a = emojis::get("😀").unwrap();
36+
let b = emojis::get("😃").unwrap();
37+
assert!(a != b);
38+
assert!(b != a);
39+
assert!(a == a);
40+
assert!(b == b);
41+
assert!(a != "😃");
42+
assert!(b == "😃");
43+
}
44+
45+
#[test]
46+
fn emoji_ord() {
47+
let a = emojis::get("😀").unwrap();
48+
let b = emojis::get("😃").unwrap();
49+
assert_eq!(a.partial_cmp(b), Some(Ordering::Less));
50+
assert_eq!(b.partial_cmp(a), Some(Ordering::Greater));
51+
assert_eq!(a.partial_cmp(a), Some(Ordering::Equal));
52+
assert_eq!(b.partial_cmp(b), Some(Ordering::Equal));
53+
assert_eq!(a.cmp(b), Ordering::Less);
54+
assert_eq!(b.cmp(a), Ordering::Greater);
55+
assert_eq!(a.cmp(a), Ordering::Equal);
56+
assert_eq!(b.cmp(b), Ordering::Equal);
3457
}
3558

3659
#[test]

0 commit comments

Comments
 (0)