Hi I am new in this I try sort array on click event, and update my html at the same time, the issue is I have an array and I format in html element to show it
function contactM(contactList) {
return $.each(contactList, function(index, val){
$('#list-contact').append("<li id="+ val.chatid +" class='list group'>"+
"<div class='userAvatar' style='background: #fff url(https:\/\/swinglifestyle.com/s1sp1cture5a/"+val.members[0].picture+ "') 50%; background-size: cover;'>"+
"<div class='status sm'></div>"+
"</div>"+
"<div class='userInfo'>"+
"<div class='msg'>"+val.members[0].name +"</div>"+
"<div class='msg'>" +val.message+"</div>"+
showNotMsg(val.message)+
"<div class='date'>"+formatDate(val.newest_message) +"</div>"+
"</div>"+unreadMsgLestThem(val.unread_cnt)
+"</li>");
});
}
this print a list of chat, now I using some like this. This is my click event I call inside the function is control the view for pass the sort array
$('#by-name').on('click',function() {
contactList.sort(SortByName);
contactM(contactList);
});
for example my view before sort and click
<div>B</div>
<div>A</div>
after click and sort
<div>B</div>
<div>A</div>
<div>A</div>
<div>B</div>
the problem is in the view show me the is sort correctly but also is duplicate some one can help me please ?
$('#list-contact')for the duplicate or previously appended values, remove them, then append the sorted values.contactListarray as well?