Hi guys,
I have one question how to find third highest number in this string str="12,6,26,14,40";
can you give me sample code.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Mar 19, 2012, 1:18 AM
also try
string str = "12,6,26,14,40";
var stringArray = str.Split(',');
var k = stringArray.OrderByDescending(i => int.Parse(i)).Skip(2).Take(1).FirstOrDefault();
hope this help.
Sam HobbsPosted Mar 19, 2012, 12:34 AM
VulpesPosted Mar 18, 2012, 8:26 AM
Of course, it makes no difference in this example because, in a sequence of 5 numbers, the 3rd highest is also the 3rd lowest :)
SenthilkumarPosted Mar 18, 2012, 4:53 AM
The .net introduced some latest concepts which allows to query the value and no need to iterate the element in the object.
The LINQ and Lambda expression allows us to write the expression.
FroglegPosted Mar 18, 2012, 2:10 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace thirdHighest
{
class Program
{
static void Main(string[] args)
{
string str = "12,6,26,14,40";
string[] ss = str.Split(',');
int[] st2 = new int[ss.Length];
for (int i = 0; i < ss.Length; i++)
{
st2[i] = Convert.ToInt32(ss[i]);
}
Array.Sort(st2);
int answer = st2[st2.Length - 3];
}
}
}