Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
  int[] list = new int[] { 6,50,99,35,1,2,7};

Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.WorksheetFunction wsf = xl.WorksheetFunction;

foreach (var item in list)
       {
                  // wsf.Rank(ItemToRank,RangeOflist,Order);
//Error: wsf.Rank(item, Error in this argument what should i pass here ,0);

        double rank= wsf.Rank(item,list, 0);
    }


Input: 6,50,99,35,1,2,7
Ouput: 3,5,6,4,1,2,4
Posted
Comments
Krunal Rohit 16-Jun-15 3:17am    
What do you want? What's your exact question?

-KR
shaprpuff 16-Jun-15 3:20am    
i am getting error in 2nd argument can you tell me what to pass there in this function

Hi,

Kindly review following links for the same.

VBA/Excel RANK in C#[^]
WorksheetFunction.Rank Method (Excel)[^]
 
Share this answer
 
Hi,

You need to pass an excel range object as the second argument . Please refer below link for details on Excel Rank function.

https://msdn.microsoft.com/en-us/library/office/ff840358.aspx[^]

Please refer below code and see how Range is set and used as an argument for Rank() function .


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel= Microsoft.Office.Interop.Excel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] list = new int[] { 6, 50, 99, 35, 1, 2, 7 };

                     
           Excel.Application xlApp = new Excel.Application();
           xlApp.Visible = true;
           Excel.Workbook xlWb = xlApp.Workbooks.Add() as Excel.Workbook;
           Excel.Worksheet xlSheet = xlWb.Sheets[1] as Excel.Worksheet;
           Microsoft.Office.Interop.Excel.WorksheetFunction wsf1 = xlApp.WorksheetFunction;


           Excel.Range range = xlSheet.get_Range("A1:A7");

           for (int i = 0; i < list.Count();i++)
           {
               range.Cells[i+1] = list[i];
              
           }
            
           for (int i = 0; i < list.Count(); i++)
           {
               Console.WriteLine(" {0} -  Rank is  : {1} ", list[i] ,wsf1.Rank(list[i], range.get_Range("A1:A7"),0));
               
           }

             //  xlWb.Close(SaveChanges:false);
            //   xlApp.Visible = false;
             //  xlApp = null;

            Console.ReadKey();
        }
    }
}
 
Share this answer
 
I do not recommend to use any specific library, unless it's standard .net.

Based on your requirements, i'd suggest to use "index":
C#
int[] list = new int[] {6,50,99,35,1,2,7};

var qry = list
            .OrderBy(i=>i)
            .Select((i, index)=>new
                {
                    number = i,
                    rank = index+1
                });


Result:
VB
number rank
1      1
2      2
6      3
7      4
35     5
50     6
99     7


Note, you can always write extension method[^].
 
Share this answer
 

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