简体   繁体   中英

How to replace the indices with the value from another array

As you can see in following script, after calculation the results.Add(dotproduct(userseq, z)); , it store the result in a list, sort the list based on the values and show the result index of values before sorting (the original index) and the values like as follow before sorting:

0  0.235
1  0.985
2  0.342
3  0.548
4  0.754

After sorting:

1  0.985
4  0.754
3  0.548
2  0.342
0  0.235

Now, i must replace the sorted index (indices after sorting) to the another value. I have an array (one dimensional) that I must compare the indices of sorted item with that array and if the indices were the same, i read the value of that indices and replace it as indices with is sorted after sorting. like

1  0.985
4  0.754
3  0.548
2  0.342
0  0.235

one dimensional Array which the indices are implicit.

0  672534
1  234523
2  567808
3  876955
4  89457

Finally the result must be like

234523  0.985
89457   0.754
876955  0.548
567808  0.342

int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
//int userseq=Array.IndexOf(d, sc);
for (int yu = 0; yu <= 92161; yu++)
{
    int wer = d[yu];
    if (wer == sc)
    {
        int userseq = yu;
    }
}
var results = new List<float>(1143600);
for (int z = 0; z < 1143600; z++)
{
    results.Add(dotproduct(userseq, z));
}
var sb1 = new StringBuilder();
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
{
    sb1.AppendFormat("{0}: {1}", resultwithindex.Index, resultwithindex.result);
    sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());

I would store your values as KeyValuePair data types

// Let's create the list that will store our information
// Keys will be ints, values the doubles
var myList = new List<KeyValuePair<int, double>>();

/*
Here is where you will load the values as you desire
This is up to you! Just add them as KeyValuePair objects to your list
(234523, 0.985), (89457, 0.754), (876955, 0.548), (567808, 0.342)
*/
// For example, I'll just add one:
myList.Add(new KeyValuePair<int, double>(567808, 0.342));

// Once you have created your list of desired KeyValuePairs, let's sort them.
// This will sort from high -> low as your example showed
myList.Sort((x, y) => y.Value.CompareTo(x.Value));

So then you are left with a sorted list of type KeyValuePair<int, double> that you can do with as you please.

... you can learn more about the KeyValuePair type here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM