java - Sort a list depends on another sorted list which has one attribute in common for sorting -
sorry long description tried explain content precisely,
i have 2 lists of different objects like,
class company { int id; string name; /* getters , setters */ } class companybysupplier { string nameid; long stakevalue; long incomevalue; /* getters , setters */ } common attribute in these list is, id of company , nameid of companybysupplier
listtosort list of objects of companybysupplier , sortedlist list of objects of company,
list<companybysupplier> listtosort = {["id3,546456,56464"],["id4",4565,456456],["id1,1234,2345"],["id2",4335,34535],["id5,345,546"]} list<company> sortedlist = {["id1","a"],["id2","b"],["id3","c"],["id4","d"],["id5","e"]} if observe sortedlist sorted depending on name attribute want listosort indirectly.
that means, need listtosort in order of sortedlist comparison of id of company , nameid of companybysupplier
so expected output should be,
list listtosort = {["id1,1234,2345"],["id2",4335,34535],["id3,546456,56464"],["id4",4565,456456],["id5,345,546"]} because id1,id2,id3,id4,id5 order in sortedlist of company.
what can think of creating new list comparing id , nameid of 2 lists contains new type of object like,
class companydata { string actualcompanyname; long stakevalue; long incomevalue; } and use comparator on actualcompanyname solve problem. i'm interested in knowing there other better way of doing this
your sorted list sorted on base of id. can sort second list on base of same id.
list<companybysupplier> listtosort = new list<companybysupplier> { new companybysupplier() { nameid = "id3", stakevalue = "546456", incomevalue = "56464" }, new companybysupplier() { nameid = "id4", stakevalue = "546456", incomevalue = "56464" }, new companybysupplier() { nameid = "id2", stakevalue = "546456", incomevalue = "56464" }, new companybysupplier() { nameid = "id1", stakevalue = "2345", incomevalue = "2346" } }; list<company> sortedlist = new list<company>() { new company(){ id = "id1", name = "a" }, new company() { id = "id2", name = "b" }, new company() { id = "id3", name = "c" } }; listtosort.sort(delegate(companybysupplier x, companybysupplier y) { return x.nameid.compareto(y.nameid); }); foreach (var item in listtosort) { console.writeline(item.nameid); } console.readkey(); } } public class company { public string id { get; set; } public string name { get; set; } } public class companybysupplier { public string nameid; public string stakevalue; public string incomevalue; }
Comments
Post a Comment