We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

# Re-arrange List of Integers in C# RSS

## 5 replies

Last post Jun 09, 2015 05:07 PM by JohnBert

• ramll

Member

554 Points

1504 Posts

### Re-arrange List of Integers in C#

My scenario is like this. Don't know if this makes sense or not but

I want to convert

1, 3, 3, 4, 5   To 1, 2, 3, 4, 5

1, 2, 4, 4, 4  To 1, 2, 3, 4, 5

1, 1, 1, 2, 3  To 1, 2, 3, 4, 5

3, 4, 5, 5, 5  To 1, 2, 3, 4, 5

So, basically what I want is check if the sequence starts with 1 or not and check whether the integer sequence is in order or not

• Siva Krishna...

Star

9052 Points

2255 Posts

### Re: Re-arrange List of Integers in C#

Jun 09, 2015 11:54 AM|Siva Krishna Macha|LINK

I couldn't get it. Are you trying to just replace any 5 numbers to 1 2 3 4 5 ? That doesn't look like any conversion logic.

Mark the post as answer, if it helps you.

Thanks & Regards,
Siva
• ryanbesko

Contributor

7058 Points

2189 Posts

### Re: Re-arrange List of Integers in C#

```var list = new List<int>() { 3, 4, 4, 4, 5 };
var starting = list.First();

for (var i = 1; i < list.Count(); i++)
{
list[i] = starting + i;
}
```

Mark all posts that give the desired result the answer. If you only mark the last that gave you clarification because you misread an earlier post others will be confused. Some of us are here to help others and our point to post ratio matters.
• gerrylowry

Star

14297 Points

5797 Posts

### Re: Re-arrange List of Integers in C#

as i former community college teacher, this seems like a homework problem.

after that, you need to edit your question.

you have not even shown your peers here at forums.asp.net what attempts you've made already ... you'll get more help if you can show that you've made an effort to solve this yourself.

TIMTOWTDI

one idea you could try is to copy your list to a separate list and order the copy; then compare your original list to your ordered version; if they are equal, then the original list was in order.

FWIW

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• PatriceSc

All-Star

48930 Points

18279 Posts

### Re: Re-arrange List of Integers in C#

Hi,

So it seems you want to "compare" rather than really to "convert"?  Looks like homework. It would be best to try something and ask for help rather than having someone doing it for you. You'll learn more by trying yourself.

1, 2, 3, 4, 5 are consecutive so you coud create a loop. Inside the loop you could check the corresponding number found in the list. For now my understanding is that you want to check if a list of numbers is 1,2,3,4,5 rather than really to "convert" a list into another list (I don't see how you woould always end up with the same 1,2,3,4,5 list).

• JohnBert

Member

320 Points

111 Posts

### Re: Re-arrange List of Integers in C#

```namespace sorting
{
class Program
{
public static List<student> students = new List<student>(); //list object

static void Main(string[] args)
{
students.Sort();
foreach (student n in students)
{
Console.WriteLine(n.name);
}

}
}

public class student:IComparable<student>
{
public string name;
public int age;
public student(string name, int age)
{
this.name = name;
this.age = age;
}
public int CompareTo(student b)
{
// Alphabetic sort name[A to Z]
return this.name.CompareTo(b.name);
}

}
}```

When a class implements the

`IComparable`

interface, we must also implement the public method

`CompareTo(T)`

. In the

`CompareTo`

method, we can write our sorting algorithm. In here I have sorted the list on student name alphabetically.

We use

`IComparable<T>`

when the class has an intrinsic comparison. We need to know the sorting criteria before we start. In here we have to decide whether to sort on age or name before we implement the class. But in some situations we may need various kinds of sorting on a class.

To solve this problem, .NET provides a special interface called

`IComparer<> `

which has a method

`Compare()`

, that takes two object parameters

`X`

,

`Y `

and returns an

`int`

. The use of the

`IComparer<> `

interface tells

`List `

how exactly you want to sort.

### IComparer

We use

`IComparer`

when you want a comparison method other than the class intrinsic comparison, if it has one.

<div class="pre-action-link" id="premain513287">Hide   Shrink   Copy Code</div>

```namespace sorting
{
class Program
{
public static List<student> students = new List<student>(); //list object

static void Main(string[] args)
{

sortOnAge soa = new sortOnAge();
students.Sort(soa);
foreach (student n in students)
{
Console.WriteLine(n.name);
}

}
}

public class student:IComparable<student>
{
public string name;
public int age;
public student(string name, int age)
{
this.name = name;
this.age = age;
}
public int CompareTo(student b)
{
// Alphabetic sort name[A to Z]
return this.name.CompareTo(b.name);
}

}

public class sortOnAge : IComparer<student>
{
public int Compare(student a, student b)
{
if (a.age > b.age) return 1;
else if (a.age < b.age) return -1;
else return 0;
}
}
}```