var map = new Map();
map.set('0001', {lastChat: {message: 'test', time: 1640063014931}});
map.set('0002', {lastChat: {message: 'test', time: 1640063014930}});
var sortedMap = new Map([...map].sort((a, b) => a[1].lastChat.time - b[1].lastChat.time));
console.log(sortedMap.entries())
//Output : MapIterator {'0002' => {…}, '0001' => {…}}