fix: sort created_at clash, closes #22
Additionally sort by name, so that if a clash of first comparison, the order will be consistent
This commit is contained in:
+47
-44
@@ -156,78 +156,81 @@ impl AppData {
|
|||||||
self.sorted_by
|
self.sorted_by
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sort the containers vec, based on a heading, either ascending or descending,
|
/// Sort the containers vec, based on a heading (and if clash, then by name), either ascending or descending,
|
||||||
/// If not sort set, then sort by created time
|
/// If not sort set, then sort by created time
|
||||||
pub fn sort_containers(&mut self) {
|
pub fn sort_containers(&mut self) {
|
||||||
if let Some((head, ord)) = self.sorted_by {
|
if let Some((head, ord)) = self.sorted_by {
|
||||||
|
let sort_closure = |a: &ContainerItem, b: &ContainerItem| -> std::cmp::Ordering {
|
||||||
match head {
|
match head {
|
||||||
Header::State => match ord {
|
Header::State => match ord {
|
||||||
SortedOrder::Asc => self
|
SortedOrder::Asc => {
|
||||||
.containers
|
a.status.cmp(&b.status).then_with(|| a.name.cmp(&b.name))
|
||||||
.items
|
}
|
||||||
.sort_by(|a, b| b.state.order().cmp(&a.state.order())),
|
SortedOrder::Desc => {
|
||||||
SortedOrder::Desc => self
|
b.status.cmp(&a.status).then_with(|| b.name.cmp(&a.name))
|
||||||
.containers
|
}
|
||||||
.items
|
|
||||||
.sort_by(|a, b| a.state.order().cmp(&b.state.order())),
|
|
||||||
},
|
},
|
||||||
Header::Status => match ord {
|
Header::Status => match ord {
|
||||||
SortedOrder::Asc => self
|
SortedOrder::Asc => {
|
||||||
.containers
|
a.status.cmp(&b.status).then_with(|| a.name.cmp(&b.name))
|
||||||
.items
|
}
|
||||||
.sort_by(|a, b| a.status.cmp(&b.status)),
|
SortedOrder::Desc => {
|
||||||
SortedOrder::Desc => self
|
b.status.cmp(&a.status).then_with(|| b.name.cmp(&a.name))
|
||||||
.containers
|
}
|
||||||
.items
|
|
||||||
.sort_by(|a, b| b.status.cmp(&a.status)),
|
|
||||||
},
|
},
|
||||||
Header::Cpu => match ord {
|
Header::Cpu => match ord {
|
||||||
SortedOrder::Asc => self
|
SortedOrder::Asc => a
|
||||||
.containers
|
.cpu_stats
|
||||||
.items
|
.back()
|
||||||
.sort_by(|a, b| a.cpu_stats.back().cmp(&b.cpu_stats.back())),
|
.cmp(&b.cpu_stats.back())
|
||||||
SortedOrder::Desc => self
|
.then_with(|| a.name.cmp(&b.name)),
|
||||||
.containers
|
SortedOrder::Desc => b
|
||||||
.items
|
.cpu_stats
|
||||||
.sort_by(|a, b| b.cpu_stats.back().cmp(&a.cpu_stats.back())),
|
.back()
|
||||||
|
.cmp(&a.cpu_stats.back())
|
||||||
|
.then_with(|| b.name.cmp(&a.name)),
|
||||||
},
|
},
|
||||||
Header::Memory => match ord {
|
Header::Memory => match ord {
|
||||||
SortedOrder::Asc => self
|
SortedOrder::Asc => a
|
||||||
.containers
|
.mem_stats
|
||||||
.items
|
.back()
|
||||||
.sort_by(|a, b| a.mem_stats.back().cmp(&b.mem_stats.back())),
|
.cmp(&b.mem_stats.back())
|
||||||
SortedOrder::Desc => self
|
.then_with(|| a.name.cmp(&b.name)),
|
||||||
.containers
|
SortedOrder::Desc => b
|
||||||
.items
|
.mem_stats
|
||||||
.sort_by(|a, b| b.mem_stats.back().cmp(&a.mem_stats.back())),
|
.back()
|
||||||
|
.cmp(&a.mem_stats.back())
|
||||||
|
.then_with(|| b.name.cmp(&a.name)),
|
||||||
},
|
},
|
||||||
Header::Id => match ord {
|
Header::Id => match ord {
|
||||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.id.cmp(&b.id)),
|
SortedOrder::Asc => a.id.cmp(&b.id).then_with(|| a.name.cmp(&b.name)),
|
||||||
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.id.cmp(&a.id)),
|
SortedOrder::Desc => b.id.cmp(&a.id).then_with(|| b.name.cmp(&a.name)),
|
||||||
},
|
},
|
||||||
Header::Image => match ord {
|
Header::Image => match ord {
|
||||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.image.cmp(&b.image)),
|
SortedOrder::Asc => a.image.cmp(&b.image).then_with(|| a.name.cmp(&b.name)),
|
||||||
SortedOrder::Desc => {
|
SortedOrder::Desc => {
|
||||||
self.containers.items.sort_by(|a, b| b.image.cmp(&a.image));
|
b.image.cmp(&a.image).then_with(|| b.name.cmp(&a.name))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Header::Name => match ord {
|
Header::Name => match ord {
|
||||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.name.cmp(&b.name)),
|
SortedOrder::Asc => a.name.cmp(&b.name).then_with(|| a.name.cmp(&b.name)),
|
||||||
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.name.cmp(&a.name)),
|
SortedOrder::Desc => b.name.cmp(&a.name).then_with(|| b.name.cmp(&a.name)),
|
||||||
},
|
},
|
||||||
Header::Rx => match ord {
|
Header::Rx => match ord {
|
||||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.rx.cmp(&b.rx)),
|
SortedOrder::Asc => a.rx.cmp(&b.rx).then_with(|| a.name.cmp(&b.name)),
|
||||||
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.rx.cmp(&a.rx)),
|
SortedOrder::Desc => b.rx.cmp(&a.rx).then_with(|| b.name.cmp(&a.name)),
|
||||||
},
|
},
|
||||||
Header::Tx => match ord {
|
Header::Tx => match ord {
|
||||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.tx.cmp(&b.tx)),
|
SortedOrder::Asc => a.tx.cmp(&b.tx).then_with(|| a.name.cmp(&b.name)),
|
||||||
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.tx.cmp(&a.tx)),
|
SortedOrder::Desc => b.tx.cmp(&a.tx).then_with(|| b.name.cmp(&a.name)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
self.containers.items.sort_by(sort_closure);
|
||||||
} else {
|
} else {
|
||||||
self.containers
|
self.containers
|
||||||
.items
|
.items
|
||||||
.sort_by(|a, b| a.created.cmp(&b.created));
|
.sort_by(|a, b| a.created.cmp(&b.created).then_with(|| a.name.cmp(&b.name)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user