У меня есть приложение, в котором пользователь создает учетные записи. Мне нужно отсортировать учетные записи по их балансу.
. Я думал о том, чтобы сохранить сами объекты учетных записей и остатки в виде listDictionary, затем Я хотел бы создать список сальдо и отсортировать их
, а затем использовать словарь списка с балансами и счетами, используя баланс в качестве ключа. Я бы поместил объект счета, связанный с балансом, обратно в список, в котором они находятся. хранится в правильном порядке.
Я не совсем уверен, как получить список ключей из списка словаря. Я знаю это listdictionary.keys, но могу ли я создать пустой список и использовать foreach?
list<double> sortinglist = new list<double>();
Foreach (double d in listdictionary.keys)
{
sortinglist.Add(d)
sortinglist.sort()
}
Я почти уверен, что это сработает, но я действительно застрял в том, что если я это сделаю и буду работать, как мне перейти из отсортированного списка к получению объектов моей учетной записи, связанных с теперь отсортированные ключи в учетной записи в правильном порядке, я вычислил лямбда-выражение какого-то рода, но я не Теперь, как структурировать один. Я только начинаю понимать, как это сделать.
Если нет, то это мой другой способ подумать о сортировке вещей:
private void button6_Click(object sender, EventArgs e)
{
double greatestBalance = 0; // this is the current greatest balance it isn't really important
int greatestIndex = 0; // the index of the current account with greatest balance
int orderCount = 0;
List<Account> sorted = new List<Account>(); // a new list to add the items in sorted order
if (ByrefButton.Checked == true); // the button that is clicked on the form
{
List<Account> copyData = data; //a copy of the list of objects so i dont damage original
for (int j = 0; j < copyData.Count; j++) // this is the first loop for every item in the copy
{ // for the first item it will run the inside loop 1ce
for (int i = 0; i < data.Count; i++)//on one run of the outside loop this loop should
{ //run through completly
double currentBalance = data[i].getBalance();
if (currentBalance > greatestBalance) // sets the largest balance not important
{
greatestBalance = currentBalance;
greatestIndex = i; //the index of the object with the largest
//balance in the data list
}
}
copyData.RemoveAt(greatestIndex); //this removes the object at the index in the copy
sorted.Add(data[greatestIndex]); //array to when the outer loop runs a second time
} // the value that had been the largest is removed.
Будет ли это работать? есть ли способ улучшить это?