Reputation: 20468
please see the list below :
string[] Separator = new string[] { "__" };
string[] lines_acc = File.ReadAllLines(accfilePath);
List<string> list_lines_acc = new List<string>(lines_acc);
List<string> list_lines_silver_count = new List<string>();
FileStream fs_ = null;
if (!File.Exists(silver_countfilePath))
{
using (fs_ = File.Create(silver_countfilePath))
{
foreach (string line_acc in list_lines_acc)
{
string[] line_acc_ar = line_acc.Split(Separator, StringSplitOptions.None);
string line_acc_new = line_acc_ar[0] + "__" + line_acc_ar[1] + "__" + line_acc_ar[3] + "__" + line_acc_ar[4] + "__" + "0";
list_lines_silver_count.Add(line_acc_new);
}
File.WriteAllLines(silver_countfilePath, list_lines_silver_count);
}
}
else
{
string[] lines_silver_count = File.ReadAllLines(silver_countfilePath);
list_lines_silver_count = new List<string>(lines_silver_count);
}
i want to sort list_lines_silver_count
by line_acc_ar[4]
part!
that part is a string like -> 325423 -> mean i can convert it to an integer.
how can i do that job?
Upvotes: 0
Views: 104
Reputation: 382
public class MyCompare : IComparer<string>
{
public int Compare(string x, string y)
{
//get line_acc_ar[4] part from strings x and y
string[] Separator = new string[] { "__" };
string partX = x.Split(Separator, StringSplitOptions.None)[3];
string partY = y.Split(Separator, StringSplitOptions.None)[3];
int intPartX = int.Parse(partX );
int inrPartY = int.Parse(partY );
return intPartX.CompareTo(inrPartY)
}
}
list_lines_silver_count.OrderBy(a => a, new MyCompare());
Upvotes: 0
Reputation: 460098
You can use Linq:
int number = 0;
string[] lines_silver_count = File
.ReadLines(silver_countfilePath)
.Select(l => new {
Line = l,
Parts = l.Split(Separator, StringSplitOptions.None)
})
.Where(x => x.Parts.Length > 4
&& int.TryParse(x.Parts[4], out number))
.OrderBy(x => number)
.Select(x => x.Line)
.ToArray();
Upvotes: 1
Reputation: 9869
You can try this
sorted using a Comparison generic delegate representing the CompareStringByInteger method
public static int CompareStringByInteger(string x, string y)
{
if (x == null)
{
if (y == null)
return 0;
else
return -1;
}
else
{
try{
return Convert.ToInt32(x).CompareTo(Convert.ToInt32(y));
}catch{
return x.CompareTo(y);
}
}
}
and apply it in sort method.
list_lines_silver_count.Sort(CompareStringByInteger);
Upvotes: 1
Reputation: 12513
Change the foreach
:
foreach (var line_acc_ar in list_lines_acc
.Select(s => s.Split(Separator, StringSplitOptions.None)
.OrderBy(a => a[4])) {
}
Further refactorings could make the code more elegant, but I think this piece of LINQ should do the job.
Upvotes: 1
Reputation: 56688
One way is to implement the comparer and provide it as an argument to the Sort
function:
public class SilverCountLineComparer : IComparer<string>
{
public int Compare(string x, string y)
{
string xPart = x.Split(new char[] {'_'}, StringSplitOptions.RemoveEmptyEntries)[3];
string yPart = y.Split(new char[] {'_'}, StringSplitOptions.RemoveEmptyEntries)[3];
int xNum = Int32.Parse(xPart);
int yNum = Int32.Parse(yPart);
return xNum.CompareTo(yNum);
}
}
And to sort call it like this:
list_lines_silver_count.Sort(new SilverCountLineComparer());
Upvotes: 1