I have a list inside a list and I am having problem sorting the string timestamp inside the list. Here is the method that retrieves the timestamp in database:
public static List<List<String>> getLogs(){
sql = new String("SELECT EmpID, ValidDate, GROUP_CONCAT(ValidTime) AS logTime FROM logs WHERE Processed = 0 GROUP BY EmpID, ValidDate");
List<List<String>> values = new ArrayList<>();
List<String> id = new ArrayList<>();
List<String> valDate = new ArrayList<>();
List<String> logs = new ArrayList<>();
try{
stmt = data.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
id.add(rs.getString("EmpID"));
valDate.add(String.valueOf(rs.getDate("ValidDate")));
logs.add(rs.getString("logTime"));
}
}catch(SQLException ex){
ex.printStackTrace();
}
values.add(id);
values.add(valDate);
values.add(logs);
return values;
}
this is the result:
2017-07-22 17:02:00,11:31:00,12:14:00
2017-07-22 11:54:00,11:01:00,17:01:00
2017-07-22 17:01:00,11:30:00,12:29:00
It's not in order.
This is how I call this method:
for(int i = 0; i < data.getLogs().get(0).size();i++){
System.out.println(data.getLogs().get(0).get(i)+" "+data.getLogs().get(1).get(i)+" "+data.getLogs().get(2).get(i));
}
Change your sql to
sql = new String("SELECT EmpID, ValidDate, GROUP_CONCAT(ValidTime) AS logTime
FROM logs WHERE Processed = 0 GROUP BY EmpID, ValidDate ORDER BY ValidDate");
I tried the advise of Ab Sin and search on how to sort data in MySQL and found that its possible to insert ORDER BY inside the GROUP_CONCAT so this is my revised sql statement:
SELECT EmpID,
ValidDate,
GROUP_CONCAT(ValidTime ORDER BY ValidTime ASC) AS logTime
FROM logs
WHERE Processed = 0
GROUP BY EmpID, ValidDate ORDER BY ValidDate ASC
Thanks for those who replied and helped me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.