카테고리 없음

[JAVA] List<Object> 정렬하기 Collections

해버니 2023. 11. 4. 10:24
반응형

 

 

 

 

 

 

 

이런 User 객체가 있을 때 나이가 큰 순으로 정렬이 하고 싶을 때는 어떻게 정렬해야할까?

 

 

 

 

 

package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import board.beans.User;

/**
 * List 나이순으로 정렬하기
 *
 */
public class SortingTest {

	public static void main(String[] agrs) {
		// list에 값 추가
		List<User> users = new ArrayList<User>(Arrays.asList(new User[] { new User("user1", 19), new User("user2", 19),
				new User("user3", 20), new User("user4", 18), new User("user5", 20) }));

		// 비교해서 정렬하기
		Collections.sort(users, (user1, user2) -> Integer.compare(user2.getAge(), user1.getAge()));

		// 출력하기
		for (User user : users) {
			System.out.println("Age :" + user.getAge() + ", Name :" + user.getName());
		}

	}
}

 

 

 

 

 

 

 

 

Collections.sort(users, (user1, user2) -> Integer.compare(user2.getAge(), user1.getAge()));

사용자 리스트를 나이 기준으로 내림차순으로 정렬한다. 

user2의 나이 - user1의 나이가 양수이면 user1과 user2의 위치 정렬 (서로 바껴야 함)

user2의 나이 - user1의 나이가 음수이면 user1과 user2의 위치 그대로

 

 

 

 

 

 

 

 Collections.sort메서드의 시그니처는 다음과 같다.

public static <T> void sort(List<T> list, Comparator<? super T> c)

- list : 정렬하려는 리스트

- c : 비교를 수행하는 Comprator 객체

 

 

 

 

 

 

 

 

 

 

 

 

User.java

package board.beans;

import java.security.Timestamp;

/**
 * 사용자
 * 
 * @author 4ckdn
 *
 */
public class User {
	/** 사용자 고유 ID */
	private long id;
	/** 영어 이름 */
	private String name;
	/** 한국어 이름 */
	private String fullName;
	/** 비밀번호 */
	private String password;
	/** 이메일 */
	private String email;
	/** 나이 */
	private int age;
	/** 가입 날짜 */
	private Timestamp createDate;

	/**
	 * 생성자
	 */
	public User() {
		// Do Nothing
	}

	/**
	 * 생성자
	 * 
	 * @param name
	 * @param age
	 */
	public User(String name, int age) {
		this.name = name;
		this.age = age;
	}

	/**
	 * @param id
	 * @param name
	 * @param fullName
	 * @param password
	 * @param email
	 * @param age
	 * @param createDate
	 */
	public User(long id, String name, String fullName, String password, String email, int age, Timestamp createDate) {
		super();
		this.id = id;
		this.name = name;
		this.fullName = fullName;
		this.password = password;
		this.email = email;
		this.age = age;
		this.createDate = createDate;
	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the fullName
	 */
	public String getFullName() {
		return fullName;
	}

	/**
	 * @param fullName the fullName to set
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * @return the createDate
	 */
	public Timestamp getCreateDate() {
		return createDate;
	}

	/**
	 * @param createDate the createDate to set
	 */
	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

}
반응형