티스토리 뷰
Java 에서의 정렬은 java.util.Collections 클래스의 static 메소드인 sort() 를 사용한다.
Collections 클래스에서 오버로딩 된 두 개의 sort() 메소드가 있음을 확인할 수 있는데
sort() 를 사용해 정렬하는 방법에는 Comparator 와 Comparable 인터페이스를 구현하는 방법이 있다.
1. Comparable <T> 인터페이스
sort() 메소드의 매개변수 타입을 보면 List<T> 로 제네릭 타입을 받고 있는데
이때 T는 <T extends Comparable>로, Comparable 인터페이스를 구현한 타입이어야 한다.
Comparable 인터페이스를 살펴보면
Comparable 인터페이스에는 compareTo() 메소드 하나 밖에 없으므로
이 메소드만 오버라이딩 해주면 Collections.sort() 의 매개변수로 List 를 넘겨서 객체를 정렬 할 수 있다.
예제)
VO 에 Comparable 인터페이스를 implements 한 후,
아래 조건에 따라 메소드를 작성하여 준다.
- 인자로 전달된 오브젝트가 작다면 양의 정수 반환
- 인자로 전달된 오브젝트가 크다면 음의 정수 반환
- 인자로 전달된 오브젝트와 같다면 0을 반환
public class GreengrassDefinitionModel implements Comparable {
private String arn;
private String creationTimestamp;
private String id;
private String lastUpdatedTimestamp;
private String latestVersion;
private String latestVersionArn;
private String name;
private Map<String, String> tags;
@Override
public int compareTo(GreengrassDefinitionModel o) {
LocalDateTime date1 = LocalDateTime.parse(this.lastUpdatedTimestamp.substring(0, this.lastUpdatedTimestamp.lastIndexOf("Z")));
LocalDateTime date2 = LocalDateTime.parse(o.getLastUpdatedTimestamp().substring(0, o.getLastUpdatedTimestamp().lastIndexOf("Z")));
if (date1.isBefore(date2)) {
return 1;
} else if (date1.isAfter(date2)) {
return -1;
} else {
return 0;
}
}
}
서비스 단에서 List 를 인자로 넣어주어 정렬을 실행하면
Collections.sort(apiGreengrassGroupListResponseModel.getGroups());
아래와 같이 내림차순 정렬이 된다.
{
"data": {
"groups": [
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a8209fd6-59d8-4da7-8d65",
"creationTimestamp": "2020-11-06T01:53:03.719Z",
"id": "a8209fd6-59d8-4da7-8d65",
"lastUpdatedTimestamp": "2020-11-06T01:53:03.719Z",
"latestVersion": "b3d4171a-0671-4133-ad5b",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a8209fd6-59d8-4da7-8d65/versions/b3d4171a-0671-4133-ad5b",
"name": "a-jane-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e5c73f07-dc56-4388-b9e4",
"creationTimestamp": "2020-11-05T04:46:33.104Z",
"id": "e5c73f07-dc56-4388-b9e4",
"lastUpdatedTimestamp": "2020-11-05T04:46:33.104Z",
"latestVersion": "b61ea5a6-c748-4d7c-a159",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e5c73f07-dc56-4388-b9e4/versions/b61ea5a6-c748-4d7c-a159",
"name": "injuk-gg-test-1105"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/d66f69d8-200a-4c72-acc0",
"creationTimestamp": "2020-11-05T04:44:50.805Z",
"id": "d66f69d8-200a-4c72-acc0",
"lastUpdatedTimestamp": "2020-11-05T04:44:50.805Z",
"latestVersion": "816577cc-dbb3-416a-a724",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/d66f69d8-200a-4c72-acc0/versions/816577cc-dbb3-416a-a724",
"name": "old-gg-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e77962b1-f922-4f30-ba9b",
"creationTimestamp": "2020-10-29T10:01:21.292Z",
"id": "e77962b1-f922-4f30-ba9b",
"lastUpdatedTimestamp": "2020-10-29T10:01:21.292Z",
"latestVersion": "ae694803-ef6d-411a-bacf",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e77962b1-f922-4f30-ba9b/versions/ae694803-ef6d-411a-bacf",
"name": "david-greengrass-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7ad579e3-db1d-4574-9a86",
"creationTimestamp": "2020-10-28T08:39:42.831Z",
"id": "7ad579e3-db1d-4574-9a86",
"lastUpdatedTimestamp": "2020-10-28T08:39:42.831Z",
"latestVersion": "e13d7c9d-7a0b-46fe-8500",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7ad579e3-db1d-4574-9a86/versions/e13d7c9d-7a0b-46fe-8500",
"name": "greengrass-gg-group-test-19"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a33d14dd-d719-470e-a5e4",
"creationTimestamp": "2020-10-28T08:37:59.350Z",
"id": "a33d14dd-d719-470e-a5e4",
"lastUpdatedTimestamp": "2020-10-28T08:37:59.350Z",
"latestVersion": "1c864125-5eac-40bf-a49f",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a33d14dd-d719-470e-a5e4-831a9cb17f91/versions/1c864125-5eac-40bf-a49f",
"name": "greengrass-gg-group-test-18"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/90fbc50a-a785-474d-b20a",
"creationTimestamp": "2020-10-27T11:38:12.450Z",
"id": "90fbc50a-a785-474d-b20a",
"lastUpdatedTimestamp": "2020-10-27T11:38:12.450Z",
"latestVersion": "c5398491-732f-43a4-ac85",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/90fbc50a-a785-474d-b20a/versions/c5398491-732f-43a4-ac85",
"name": "greengrass-gg-group-test-11"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/934ce924-5adb-447c-9c44",
"creationTimestamp": "2020-10-27T11:37:28.208Z",
"id": "934ce924-5adb-447c-9c44",
"lastUpdatedTimestamp": "2020-10-27T11:37:28.208Z",
"latestVersion": "5a4613b5-eb4e-4a8d-be89",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/934ce924-5adb-447c-9c44/versions/5a4613b5-eb4e-4a8d-be89",
"name": "greengrass-gg-group-test-10"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e6fb2af0-9001-4448-8de2",
"creationTimestamp": "2020-10-27T11:15:59.793Z",
"id": "e6fb2af0-9001-4448-8de2",
"lastUpdatedTimestamp": "2020-10-27T11:15:59.793Z",
"latestVersion": "8f03ca0c-58a1-4397-9ddd",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e6fb2af0-9001-4448-8de2/versions/8f03ca0c-58a1-4397-9ddd",
"name": "Greengrass_test-scenario_3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5025c20c-7fbd-4041-9c8f",
"creationTimestamp": "2020-10-14T04:34:53.634Z",
"id": "5025c20c-7fbd-4041-9c8f",
"lastUpdatedTimestamp": "2020-10-14T04:34:53.634Z",
"latestVersion": "dca47638-3e5f-4760-9cd1",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5025c20c-7fbd-4041-9c8f/versions/dca47638-3e5f-4760-9cd1",
"name": "Greengrass_test-scenario_2-1"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/1d3ec07d-1f7f-471b-95ef",
"creationTimestamp": "2020-10-14T04:01:28.059Z",
"id": "1d3ec07d-1f7f-471b-95ef",
"lastUpdatedTimestamp": "2020-10-14T04:01:28.059Z",
"latestVersion": "a6865ff6-91ff-422a-95b5",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/1d3ec07d-1f7f-471b-95ef/versions/a6865ff6-91ff-422a-95b5",
"name": "Greengrass_test-scenario_2"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/62259db0-ff35-4a17-8dbc",
"creationTimestamp": "2020-09-24T02:57:17.930Z",
"id": "62259db0-ff35-4a17-8dbc",
"lastUpdatedTimestamp": "2020-09-24T02:57:17.930Z",
"latestVersion": "c38990ee-d71b-4624-bbb9",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/62259db0-ff35-4a17-8dbc/versions/c38990ee-d71b-4624-bbb9",
"name": "test-group-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/38e62a05-2c92-4a26-8bb4",
"creationTimestamp": "2020-09-24T02:55:24.356Z",
"id": "38e62a05-2c92-4a26-8bb4",
"lastUpdatedTimestamp": "2020-09-24T02:55:24.356Z",
"latestVersion": "4435958e-68cf-4b0f-8427",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/38e62a05-2c92-4a26-8bb4/versions/4435958e-68cf-4b0f-8427",
"name": "test-gg-group-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/4a6a657b-137f-4739-acbb",
"creationTimestamp": "2020-09-21T08:13:48.981Z",
"id": "4a6a657b-137f-4739-acbb",
"lastUpdatedTimestamp": "2020-09-21T08:13:48.981Z",
"latestVersion": "d05faea7-e42d-4288-9f9e",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/4a6a657b-137f-4739-acbb/versions/d05faea7-e42d-4288-9f9e",
"name": "thor-test"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bc977c6c-32b8-45fd-aaf8",
"creationTimestamp": "2020-09-15T04:58:56.150Z",
"id": "bc977c6c-32b8-45fd-aaf8",
"lastUpdatedTimestamp": "2020-09-15T04:58:56.150Z",
"latestVersion": "ff8cd944-12ae-4d1b-98c6",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bc977c6c-32b8-45fd-aaf8/versions/ff8cd944-12ae-4d1b-98c6",
"name": "gg-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/ad353ccb-70a3-4e7c-ab78",
"creationTimestamp": "2020-09-07T06:47:27.211Z",
"id": "ad353ccb-70a3-4e7c-ab78",
"lastUpdatedTimestamp": "2020-09-07T06:47:27.211Z",
"latestVersion": "599db661-16b1-46ee-b726",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/ad353ccb-70a3-4e7c-ab78/versions/599db661-16b1-46ee-b726",
"name": "thor-test-01"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5473fbd8-a354-4b62-a088",
"creationTimestamp": "2020-09-02T23:55:25.772Z",
"id": "5473fbd8-a354-4b62-a088",
"lastUpdatedTimestamp": "2020-09-02T23:55:25.772Z",
"latestVersion": "de5c2007-a2b0-47ac-a373",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5473fbd8-a354-4b62/versions/de5c2007-a2b0-47ac-a373",
"name": "test1234"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/2a52b008-1b5b-40b2-8201",
"creationTimestamp": "2020-08-28T01:11:21.977Z",
"id": "2a52b008-1b5b-40b2-8201",
"lastUpdatedTimestamp": "2020-08-28T01:11:21.977Z",
"latestVersion": "1d766578-1a10-4245-ae71",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/2a52b008-1b5b-40b2-8201/versions/1d766578-1a10-4245-ae71",
"name": "greengrass-eric-test-4"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/419f8be9-fbc9-40a9-8a46",
"creationTimestamp": "2020-08-27T12:42:06.016Z",
"id": "419f8be9-fbc9-40a9-8a46",
"lastUpdatedTimestamp": "2020-08-27T12:42:06.016Z",
"latestVersion": "c5aaf50a-6760-4f78-afaf",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/419f8be9-fbc9-40a9-8a46/versions/c5aaf50a-6760-4f78-afaf",
"name": "eric-test-greengrass-1"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/0fbb56cc-72de-4852-9ba3",
"creationTimestamp": "2020-08-27T02:06:13.071Z",
"id": "0fbb56cc-72de-4852-9ba3",
"lastUpdatedTimestamp": "2020-08-27T02:06:13.071Z",
"latestVersion": "97bdf02c-82e9-4d15-8aa6",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/0fbb56cc-72de-4852-9ba3/versions/97bdf02c-82e9-4d15-8aa6",
"name": "GG_eric_test"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7dfca9ec-fb27-4cc4-b356",
"creationTimestamp": "2020-07-14T06:05:10.452Z",
"id": "7dfca9ec-fb27-4cc4-b356",
"lastUpdatedTimestamp": "2020-07-14T06:05:10.452Z",
"latestVersion": "f0cec328-963e-4cd2-8291",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7dfca9ec-fb27-4cc4-b356/versions/f0cec328-963e-4cd2-8291",
"name": "eric-test-greengrass-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bf8ffbdc-fab6-417b-bf58",
"creationTimestamp": "2020-07-14T05:54:23.936Z",
"id": "bf8ffbdc-fab6-417b-bf58",
"lastUpdatedTimestamp": "2020-07-14T05:54:23.936Z",
"latestVersion": "b44ae48b-be02-4750-ae32",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bf8ffbdc-fab6-417b-bf58/versions/b44ae48b-be02-4750-ae32",
"name": "eric-test-greengrass-2"
}
]
}
}
여기서 내림차순이 아닌 오름차순 정렬을 하고싶다면,
인자 값으로 Collections.reverseOrder() 를 추가해주면 오버로딩 된다.
Collections.sort(apiGreengrassGroupListResponseModel.getGroups(), Collections.reverseOrder());
아래는 오름차순 정렬 된 결과이다.
{
"data": {
"groups": [
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bf8ffbdc-fab6-417b-bf58",
"creationTimestamp": "2020-07-14T05:54:23.936Z",
"id": "bf8ffbdc-fab6-417b-bf58",
"lastUpdatedTimestamp": "2020-07-14T05:54:23.936Z",
"latestVersion": "b44ae48b-be02-4750-ae32",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bf8ffbdc-fab6-417b-bf58/versions/b44ae48b-be02-4750-ae32",
"name": "eric-test-greengrass-2"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7dfca9ec-fb27-4cc4-b356",
"creationTimestamp": "2020-07-14T06:05:10.452Z",
"id": "7dfca9ec-fb27-4cc4-b356",
"lastUpdatedTimestamp": "2020-07-14T06:05:10.452Z",
"latestVersion": "f0cec328-963e-4cd2-8291",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7dfca9ec-fb27-4cc4-b356/versions/f0cec328-963e-4cd2-8291",
"name": "eric-test-greengrass-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/0fbb56cc-72de-4852-9ba3",
"creationTimestamp": "2020-08-27T02:06:13.071Z",
"id": "0fbb56cc-72de-4852-9ba3",
"lastUpdatedTimestamp": "2020-08-27T02:06:13.071Z",
"latestVersion": "97bdf02c-82e9-4d15-8aa6",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/0fbb56cc-72de-4852-9ba3/versions/97bdf02c-82e9-4d15-8aa6",
"name": "GG_eric_test"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/419f8be9-fbc9-40a9-8a46",
"creationTimestamp": "2020-08-27T12:42:06.016Z",
"id": "419f8be9-fbc9-40a9-8a46",
"lastUpdatedTimestamp": "2020-08-27T12:42:06.016Z",
"latestVersion": "c5aaf50a-6760-4f78-afaf",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/419f8be9-fbc9-40a9-8a46/versions/c5aaf50a-6760-4f78-afaf",
"name": "eric-test-greengrass-1"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/2a52b008-1b5b-40b2-8201",
"creationTimestamp": "2020-08-28T01:11:21.977Z",
"id": "2a52b008-1b5b-40b2-8201",
"lastUpdatedTimestamp": "2020-08-28T01:11:21.977Z",
"latestVersion": "1d766578-1a10-4245-ae71",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/2a52b008-1b5b-40b2-8201/versions/1d766578-1a10-4245-ae71",
"name": "greengrass-eric-test-4"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5473fbd8-a354-4b62-a088",
"creationTimestamp": "2020-09-02T23:55:25.772Z",
"id": "5473fbd8-a354-4b62-a088",
"lastUpdatedTimestamp": "2020-09-02T23:55:25.772Z",
"latestVersion": "de5c2007-a2b0-47ac-a373",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5473fbd8-a354-4b62-a088/versions/de5c2007-a2b0-47ac-a373",
"name": "test1234"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/ad353ccb-70a3-4e7c-ab78",
"creationTimestamp": "2020-09-07T06:47:27.211Z",
"id": "ad353ccb-70a3-4e7c-ab78",
"lastUpdatedTimestamp": "2020-09-07T06:47:27.211Z",
"latestVersion": "599db661-16b1-46ee-b726",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/ad353ccb-70a3-4e7c-ab78/versions/599db661-16b1-46ee-b726",
"name": "thor-test-01"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bc977c6c-32b8-45fd-aaf8",
"creationTimestamp": "2020-09-15T04:58:56.150Z",
"id": "bc977c6c-32b8-45fd-aaf8",
"lastUpdatedTimestamp": "2020-09-15T04:58:56.150Z",
"latestVersion": "ff8cd944-12ae-4d1b-98c6",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bc977c6c-32b8-45fd-aaf8/versions/ff8cd944-12ae-4d1b-98c6",
"name": "gg-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/4a6a657b-137f-4739-acbb",
"creationTimestamp": "2020-09-21T08:13:48.981Z",
"id": "4a6a657b-137f-4739-acbb",
"lastUpdatedTimestamp": "2020-09-21T08:13:48.981Z",
"latestVersion": "d05faea7-e42d-4288-9f9e",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/4a6a657b-137f-4739-acbb/versions/d05faea7-e42d-4288-9f9e",
"name": "thor-test"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/38e62a05-2c92-4a26-8bb4",
"creationTimestamp": "2020-09-24T02:55:24.356Z",
"id": "38e62a05-2c92-4a26-8bb4",
"lastUpdatedTimestamp": "2020-09-24T02:55:24.356Z",
"latestVersion": "4435958e-68cf-4b0f-8427",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/38e62a05-2c92-4a26-8bb4/versions/4435958e-68cf-4b0f-8427",
"name": "test-gg-group-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/62259db0-ff35-4a17-8dbc",
"creationTimestamp": "2020-09-24T02:57:17.930Z",
"id": "62259db0-ff35-4a17-8dbc",
"lastUpdatedTimestamp": "2020-09-24T02:57:17.930Z",
"latestVersion": "c38990ee-d71b-4624-bbb9",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/62259db0-ff35-4a17-8dbc/versions/c38990ee-d71b-4624-bbb9",
"name": "test-group-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/1d3ec07d-1f7f-471b-95ef",
"creationTimestamp": "2020-10-14T04:01:28.059Z",
"id": "1d3ec07d-1f7f-471b-95ef",
"lastUpdatedTimestamp": "2020-10-14T04:01:28.059Z",
"latestVersion": "a6865ff6-91ff-422a-95b5",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/1d3ec07d-1f7f-471b-95ef/versions/a6865ff6-91ff-422a-95b5",
"name": "Greengrass_test-scenario_2"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5025c20c-7fbd-4041-9c8f",
"creationTimestamp": "2020-10-14T04:34:53.634Z",
"id": "5025c20c-7fbd-4041-9c8f",
"lastUpdatedTimestamp": "2020-10-14T04:34:53.634Z",
"latestVersion": "dca47638-3e5f-4760-9cd1",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5025c20c-7fbd-4041-9c8f/versions/dca47638-3e5f-4760-9cd1",
"name": "Greengrass_test-scenario_2-1"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e6fb2af0-9001-4448-8de2",
"creationTimestamp": "2020-10-27T11:15:59.793Z",
"id": "e6fb2af0-9001-4448-8de2",
"lastUpdatedTimestamp": "2020-10-27T11:15:59.793Z",
"latestVersion": "8f03ca0c-58a1-4397-9ddd",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e6fb2af0-9001-4448-8de2/versions/8f03ca0c-58a1-4397-9ddd",
"name": "Greengrass_test-scenario_3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/934ce924-5adb-447c-9c44",
"creationTimestamp": "2020-10-27T11:37:28.208Z",
"id": "934ce924-5adb-447c-9c44",
"lastUpdatedTimestamp": "2020-10-27T11:37:28.208Z",
"latestVersion": "5a4613b5-eb4e-4a8d-be89",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/934ce924-5adb-447c-9c44/versions/5a4613b5-eb4e-4a8d-be89",
"name": "greengrass-gg-group-test-10"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/90fbc50a-a785-474d-b20a",
"creationTimestamp": "2020-10-27T11:38:12.450Z",
"id": "90fbc50a-a785-474d-b20a",
"lastUpdatedTimestamp": "2020-10-27T11:38:12.450Z",
"latestVersion": "c5398491-732f-43a4-ac85",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/90fbc50a-a785-474d-b20a/versions/c5398491-732f-43a4-ac85",
"name": "greengrass-gg-group-test-11"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a33d14dd-d719-470e-a5e4",
"creationTimestamp": "2020-10-28T08:37:59.350Z",
"id": "a33d14dd-d719-470e-a5e4",
"lastUpdatedTimestamp": "2020-10-28T08:37:59.350Z",
"latestVersion": "1c864125-5eac-40bf-a49f",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a33d14dd-d719-470e-a5e4/versions/1c864125-5eac-40bf-a49f",
"name": "greengrass-gg-group-test-18"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7ad579e3-db1d-4574-9a86",
"creationTimestamp": "2020-10-28T08:39:42.831Z",
"id": "7ad579e3-db1d-4574-9a86",
"lastUpdatedTimestamp": "2020-10-28T08:39:42.831Z",
"latestVersion": "e13d7c9d-7a0b-46fe-8500",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7ad579e3-db1d-4574-9a86/versions/e13d7c9d-7a0b-46fe-8500",
"name": "greengrass-gg-group-test-19"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e77962b1-f922-4f30-ba9b",
"creationTimestamp": "2020-10-29T10:01:21.292Z",
"id": "e77962b1-f922-4f30-ba9b",
"lastUpdatedTimestamp": "2020-10-29T10:01:21.292Z",
"latestVersion": "ae694803-ef6d-411a-bacf",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e77962b1-f922-4f30-ba9b/versions/ae694803-ef6d-411a-bacf",
"name": "david-greengrass-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/d66f69d8-200a-4c72-acc0",
"creationTimestamp": "2020-11-05T04:44:50.805Z",
"id": "d66f69d8-200a-4c72-acc0",
"lastUpdatedTimestamp": "2020-11-05T04:44:50.805Z",
"latestVersion": "816577cc-dbb3-416a-a724",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/d66f69d8-200a-4c72-acc0/versions/816577cc-dbb3-416a-a724",
"name": "old-gg-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e5c73f07-dc56-4388-b9e4",
"creationTimestamp": "2020-11-05T04:46:33.104Z",
"id": "e5c73f07-dc56-4388-b9e4",
"lastUpdatedTimestamp": "2020-11-05T04:46:33.104Z",
"latestVersion": "b61ea5a6-c748-4d7c-a159",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e5c73f07-dc56-4388-b9e4/versions/b61ea5a6-c748-4d7c-a159",
"name": "injuk-gg-test-1105"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a8209fd6-59d8-4da7-8d65",
"creationTimestamp": "2020-11-06T01:53:03.719Z",
"id": "a8209fd6-59d8-4da7-8d65",
"lastUpdatedTimestamp": "2020-11-06T01:53:03.719Z",
"latestVersion": "b3d4171a-0671-4133-ad5b",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a8209fd6-59d8-4da7-8d65/versions/b3d4171a-0671-4133-ad5b",
"name": "a-jane-test-group"
}
]
}
}
2. Comparator 인터페이스
만약 알파벳의 사전편찬 순이라던가 숫자 오름차순 같이 natural order 대로의 정렬하는 방법 말고
사용자가 원하는 대로 임의의 정렬 기준(타임스탬프순, 이름 길이순 등)대로 정렬하고 싶으면
Comparator 인터페이스를 구현해 정렬의 기준을 직접 정의해줄 수 있다.
Comparator 인터페이스를 살펴보면
Comparator 인터페이스에는 두 개의 파라미터를 넣어
정렬을 해주는 compare() 메소드가 있다.
예제)
Comparator 인터페이스를 사용하기 위해서
VO 외의 비교 메소드를 가진 클래스를 따로 생성해준다.
이 클래스는 Comparator 인터페이스를 implements 한 후,
아래 조건에 따라 compare() 메소드를 작성하여 준다.
- 인자로 전달된 오브젝트가 작다면 양의 정수 반환
- 인자로 전달된 오브젝트가 크다면 음의 정수 반환
- 인자로 전달된 오브젝트와 같다면 0을 반환
public class GroupComparator implements Comparator {
@Override
public int compare(GreengrassDefinitionModel o1, GreengrassDefinitionModel o2) {
LocalDateTime date1 = LocalDateTime.parse(o1.getLastUpdatedTimestamp().substring(0, o1.getLastUpdatedTimestamp().lastIndexOf("Z")));
LocalDateTime date2 = LocalDateTime.parse(o2.getLastUpdatedTimestamp().substring(0, o2.getLastUpdatedTimestamp().lastIndexOf("Z")));
if (date1.isBefore(date2)) {
return 1;
} else if (date1.isAfter(date2)) {
return -1;
} else {
return 0;
}
}
}
서비스 단에서 List 를 첫번째 인자로 넣어주고,
두번째 인자에 위에서 생성한 클래스를 넣어 정렬을 실행하면
Collections.sort(apiGreengrassGroupListResponseModel.getGroups(), new GroupComparator());
아래와 같이 내림차순 정렬이 된다.
{
"data": {
"groups": [
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a8209fd6-59d8-4da7-8d65",
"creationTimestamp": "2020-11-06T01:53:03.719Z",
"id": "a8209fd6-59d8-4da7-8d65",
"lastUpdatedTimestamp": "2020-11-06T01:53:03.719Z",
"latestVersion": "b3d4171a-0671-4133-ad5b",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a8209fd6-59d8-4da7-8d65/versions/b3d4171a-0671-4133-ad5b",
"name": "a-jane-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e5c73f07-dc56-4388-b9e4",
"creationTimestamp": "2020-11-05T04:46:33.104Z",
"id": "e5c73f07-dc56-4388-b9e4",
"lastUpdatedTimestamp": "2020-11-05T04:46:33.104Z",
"latestVersion": "b61ea5a6-c748-4d7c-a159",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e5c73f07-dc56-4388-b9e4/versions/b61ea5a6-c748-4d7c-a159",
"name": "injuk-gg-test-1105"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/d66f69d8-200a-4c72-acc0",
"creationTimestamp": "2020-11-05T04:44:50.805Z",
"id": "d66f69d8-200a-4c72-acc0",
"lastUpdatedTimestamp": "2020-11-05T04:44:50.805Z",
"latestVersion": "816577cc-dbb3-416a-a724",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/d66f69d8-200a-4c72-acc0/versions/816577cc-dbb3-416a-a724",
"name": "old-gg-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e77962b1-f922-4f30-ba9b-e12b4e7197db",
"creationTimestamp": "2020-10-29T10:01:21.292Z",
"id": "e77962b1-f922-4f30-ba9b",
"lastUpdatedTimestamp": "2020-10-29T10:01:21.292Z",
"latestVersion": "ae694803-ef6d-411a-bacf",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e77962b1-f922-4f30-ba9b/versions/ae694803-ef6d-411a-bacf",
"name": "david-greengrass-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7ad579e3-db1d-4574-9a86",
"creationTimestamp": "2020-10-28T08:39:42.831Z",
"id": "7ad579e3-db1d-4574-9a86",
"lastUpdatedTimestamp": "2020-10-28T08:39:42.831Z",
"latestVersion": "e13d7c9d-7a0b-46fe-8500",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7ad579e3-db1d-4574-9a86/versions/e13d7c9d-7a0b-46fe-8500",
"name": "greengrass-gg-group-test-19"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a33d14dd-d719-470e-a5e4",
"creationTimestamp": "2020-10-28T08:37:59.350Z",
"id": "a33d14dd-d719-470e-a5e4",
"lastUpdatedTimestamp": "2020-10-28T08:37:59.350Z",
"latestVersion": "1c864125-5eac-40bf-a49f",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/a33d14dd-d719-470e-a5e4/versions/1c864125-5eac-40bf-a49f",
"name": "greengrass-gg-group-test-18"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/90fbc50a-a785-474d-b20a",
"creationTimestamp": "2020-10-27T11:38:12.450Z",
"id": "90fbc50a-a785-474d-b20a",
"lastUpdatedTimestamp": "2020-10-27T11:38:12.450Z",
"latestVersion": "c5398491-732f-43a4-ac85",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/90fbc50a-a785-474d-b20a/versions/c5398491-732f-43a4-ac85",
"name": "greengrass-gg-group-test-11"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/934ce924-5adb-447c-9c44",
"creationTimestamp": "2020-10-27T11:37:28.208Z",
"id": "934ce924-5adb-447c-9c44",
"lastUpdatedTimestamp": "2020-10-27T11:37:28.208Z",
"latestVersion": "5a4613b5-eb4e-4a8d-be89",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/934ce924-5adb-447c-9c44/versions/5a4613b5-eb4e-4a8d-be89",
"name": "greengrass-gg-group-test-10"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e6fb2af0-9001-4448-8de2",
"creationTimestamp": "2020-10-27T11:15:59.793Z",
"id": "e6fb2af0-9001-4448-8de2",
"lastUpdatedTimestamp": "2020-10-27T11:15:59.793Z",
"latestVersion": "8f03ca0c-58a1-4397-9ddd",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/e6fb2af0-9001-4448-8de2/versions/8f03ca0c-58a1-4397-9ddd",
"name": "Greengrass_test-scenario_3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5025c20c-7fbd-4041-9c8f",
"creationTimestamp": "2020-10-14T04:34:53.634Z",
"id": "5025c20c-7fbd-4041-9c8f",
"lastUpdatedTimestamp": "2020-10-14T04:34:53.634Z",
"latestVersion": "dca47638-3e5f-4760-9cd1",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5025c20c-7fbd-4041-9c8f/versions/dca47638-3e5f-4760-9cd1",
"name": "Greengrass_test-scenario_2-1"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/1d3ec07d-1f7f-471b-95ef",
"creationTimestamp": "2020-10-14T04:01:28.059Z",
"id": "1d3ec07d-1f7f-471b-95ef",
"lastUpdatedTimestamp": "2020-10-14T04:01:28.059Z",
"latestVersion": "a6865ff6-91ff-422a-95b5",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/1d3ec07d-1f7f-471b-95ef/versions/a6865ff6-91ff-422a-95b5",
"name": "Greengrass_test-scenario_2"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/62259db0-ff35-4a17-8dbc",
"creationTimestamp": "2020-09-24T02:57:17.930Z",
"id": "62259db0-ff35-4a17-8dbc",
"lastUpdatedTimestamp": "2020-09-24T02:57:17.930Z",
"latestVersion": "c38990ee-d71b-4624-bbb9",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/62259db0-ff35-4a17-8dbc/versions/c38990ee-d71b-4624-bbb9",
"name": "test-group-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/38e62a05-2c92-4a26-8bb4",
"creationTimestamp": "2020-09-24T02:55:24.356Z",
"id": "38e62a05-2c92-4a26-8bb4",
"lastUpdatedTimestamp": "2020-09-24T02:55:24.356Z",
"latestVersion": "4435958e-68cf-4b0f-8427",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/38e62a05-2c92-4a26-8bb4/versions/4435958e-68cf-4b0f-8427",
"name": "test-gg-group-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/4a6a657b-137f-4739-acbb",
"creationTimestamp": "2020-09-21T08:13:48.981Z",
"id": "4a6a657b-137f-4739-acbb",
"lastUpdatedTimestamp": "2020-09-21T08:13:48.981Z",
"latestVersion": "d05faea7-e42d-4288-9f9e",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/4a6a657b-137f-4739-acbb/versions/d05faea7-e42d-4288-9f9e",
"name": "thor-test"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bc977c6c-32b8-45fd-aaf8",
"creationTimestamp": "2020-09-15T04:58:56.150Z",
"id": "bc977c6c-32b8-45fd-aaf8",
"lastUpdatedTimestamp": "2020-09-15T04:58:56.150Z",
"latestVersion": "ff8cd944-12ae-4d1b-98c6",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bc977c6c-32b8-45fd-aaf8/versions/ff8cd944-12ae-4d1b-98c6",
"name": "gg-test-group"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/ad353ccb-70a3-4e7c-ab78",
"creationTimestamp": "2020-09-07T06:47:27.211Z",
"id": "ad353ccb-70a3-4e7c-ab78",
"lastUpdatedTimestamp": "2020-09-07T06:47:27.211Z",
"latestVersion": "599db661-16b1-46ee-b726",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/ad353ccb-70a3-4e7c-ab78/versions/599db661-16b1-46ee-b726",
"name": "thor-test-01"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5473fbd8-a354-4b62-a088",
"creationTimestamp": "2020-09-02T23:55:25.772Z",
"id": "5473fbd8-a354-4b62-a088",
"lastUpdatedTimestamp": "2020-09-02T23:55:25.772Z",
"latestVersion": "de5c2007-a2b0-47ac-a373",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/5473fbd8-a354-4b62-a088/versions/de5c2007-a2b0-47ac-a373",
"name": "test1234"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/2a52b008-1b5b-40b2-8201",
"creationTimestamp": "2020-08-28T01:11:21.977Z",
"id": "2a52b008-1b5b-40b2-8201",
"lastUpdatedTimestamp": "2020-08-28T01:11:21.977Z",
"latestVersion": "1d766578-1a10-4245-ae71",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/2a52b008-1b5b-40b2-8201/versions/1d766578-1a10-4245-ae71",
"name": "greengrass-eric-test-4"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/419f8be9-fbc9-40a9-8a46",
"creationTimestamp": "2020-08-27T12:42:06.016Z",
"id": "419f8be9-fbc9-40a9-8a46",
"lastUpdatedTimestamp": "2020-08-27T12:42:06.016Z",
"latestVersion": "c5aaf50a-6760-4f78-afaf",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/419f8be9-fbc9-40a9-8a46/versions/c5aaf50a-6760-4f78-afaf",
"name": "eric-test-greengrass-1"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/0fbb56cc-72de-4852-9ba3",
"creationTimestamp": "2020-08-27T02:06:13.071Z",
"id": "0fbb56cc-72de-4852-9ba3",
"lastUpdatedTimestamp": "2020-08-27T02:06:13.071Z",
"latestVersion": "97bdf02c-82e9-4d15-8aa6",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/0fbb56cc-72de-4852-9ba3/versions/97bdf02c-82e9-4d15-8aa6",
"name": "GG_eric_test"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7dfca9ec-fb27-4cc4-b356",
"creationTimestamp": "2020-07-14T06:05:10.452Z",
"id": "7dfca9ec-fb27-4cc4-b356",
"lastUpdatedTimestamp": "2020-07-14T06:05:10.452Z",
"latestVersion": "f0cec328-963e-4cd2-8291",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/7dfca9ec-fb27-4cc4-b356/versions/f0cec328-963e-4cd2-8291",
"name": "eric-test-greengrass-3"
},
{
"arn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bf8ffbdc-fab6-417b-bf58",
"creationTimestamp": "2020-07-14T05:54:23.936Z",
"id": "bf8ffbdc-fab6-417b-bf58",
"lastUpdatedTimestamp": "2020-07-14T05:54:23.936Z",
"latestVersion": "b44ae48b-be02-4750-ae32",
"latestVersionArn": "arn:aws:greengrass:ap-northeast-2:20201225:/greengrass/groups/bf8ffbdc-fab6-417b-bf58/versions/b44ae48b-be02-4750-ae32",
"name": "eric-test-greengrass-2"
}
]
}
}
'Java' 카테고리의 다른 글
[Java] Url 파일 다운로드 (0) | 2021.02.10 |
---|---|
[Java] Timestamp 비교 (0) | 2021.02.10 |
[Java] ConcurrentHashMap, HashMap, Hashtable (0) | 2021.02.10 |
[Java] static 변수와 static 메소드 (0) | 2021.02.10 |
[Java] Enum class (0) | 2021.02.10 |