# Re-arrange List of Integers in C#

• ### Question

• User1655374113 posted

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

Tuesday, June 9, 2015 11:41 AM

### All replies

• User-484054684 posted

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.

Tuesday, June 9, 2015 11:54 AM
• User2053451246 posted
```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;
}
```

Tuesday, June 9, 2015 11:55 AM
• User-434868552 posted

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

Tuesday, June 9, 2015 11:59 AM
• User753101303 posted

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).

Tuesday, June 9, 2015 12:06 PM
• User-1078840699 posted
```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;
}
}
}```
Tuesday, June 9, 2015 5:07 PM