Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Regentag
Forked from Kroisse/gist:29b5ef06dba7b40ca12c
Created January 11, 2015 11:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Regentag/9c67e07666202d0358f8 to your computer and use it in GitHub Desktop.
Save Regentag/9c67e07666202d0358f8 to your computer and use it in GitHub Desktop.
use std::cmp::Ordering::Less;
fn sort3<T: Ord>( a: T, b: T, c: T ) -> (T,T,T)
{
// Ord trait는 cmp() 메서드를 선언한다.
match( a.cmp(&b), b.cmp(&c), c.cmp(&a) )
{
( Less, Less, _ ) => (a,b,c),
( _ , Less, Less ) => (b,c,a),
( Less, _ , Less ) => (c,a,b),
( Less, _ , _ ) => (a,c,b),
( _ , Less, _ ) => (b,a,c),
( _ , _ , Less ) => (c,b,a),
( _ , _ , _ ) => (a,b,c), // (Equal,Equal,Equal)
}
}
fn main() {
// 이하는 모두 Ord를 구현하는 타입의 값에 대한 테스트
println!("{:?}", sort3("a", "b", "c"));
println!("{:?}", sort3(200, 101, 342));
println!("{:?}", sort3("aa", "aaa", "a"));
println!("{:?}", sort3(-42, 3, 0));
println!("{:?}", sort3("c", "b", "a"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment