Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
we have 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 19th.

NOW: here is my question and exact want (Can i be able to change the highest value to be 1st and the next largest value will be 2nd, 3rd and 4th continuously within this series wherever we have similar integers we will have similar position say 19th and 19th will have 1st, 1st because they are both the largest values. but the positioning will continue as 3rd for 18th; 4th for 17th ; 5th for 16th; 6th for 15th; 7th for 14th; 8th for 13th; 9th for 12th; 10th for 11th; 11th for 10th; and 12th for 10th; 13th for 8th; etc.


is just like having 13 student in a class who took an examination and had different scores in a subject like English, when we want to rank them or say give them their result we need to calculate their position in the subject examination in such a way that we will definitely end up having some one in the class with the last position 13th even when we have student with similar position. this is what i want my button1_click event to do

thank you sir (for your help) and more help


can i be thought how to do it right please (thanks in advance) (am using c# in wpf)

What I have tried:

when my button convert is clicked with code:

C#
private void btnconvert_Click(object sender, RoutedEventArgs e)
{
foreach (int num in txtresults.ToString())
{
string[] parts = txtresults.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
double[] numbers = parts.Select(p => double.Parse(p)).ToArray();
double maximum = numbers.Max();
int position = Array.IndexOf(numbers, maximum);
parts[position] = AddOrdinal(num);
txtresults.Text = string.Join(" ", parts);
}
}



this is my method of conversion

C#
public static string AddOrdinal(int num)
{
if (num <= 0) return num.ToString();

     switch (num % 100)
     {
         case 11:
         case 12:
         case 13:
             return num + "th";
     }
     switch (num % 10)
     {
         case 1:
             return num + "st";
         case 2:
             return num + "nd";
         case 3:
             return num + "rd";
         default:
             return num + "th";
     }
 }
Posted
Updated 4-Mar-21 22:15pm
v2
Comments
[no name] 4-Mar-21 16:23pm    
Text speak denotes sloppiness, IMO. Like 19th AND 19th. Your "requirements" vaguely hint at a sort, sort of. And a progression, maybe.

1 solution

I'm not sure i understand you well, but seems you want to create something like ranking function...

Take a look at below code:
C#
int[] numbers = new int[]{8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19};
List<Tuple<int, int>> result = numbers
    .OrderByDescending(x=>x) //sort in desc order
    .GroupBy(x => x) //group by number
    .SelectMany((grp, i) => grp
        .Select(g => new Tuple<int, int>(i+1, g))) //select index of group and every single value from group
    .ToList();

Result:
Rank Value
1    19 // both 19's have the same rank
1    19 // 
2    18 
3    17 
4    16 
5    15 
6    14 
7    13 
8    12 
9    11 
10   10 
11    9 
12    8 


As you can see, above Linq query is not perfect, because 18 have to have 3th rank. Feel free to improve it.

See also: Linq query to get rank based on b[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900