I have a list which contains
- The
Price - The
Code
I’ve managed to split the list to an array, but I want to further split the array so that I can get the Price and Code separately and sort the Price to ascending order. When the sorting occurs, I need the Code to be sorted together as well because the Price is for that particular Code.
So it will be like this:
Original List:
1588,8DNY;1488,ACNY;1288,7DPE;1888,8HUC;1488,8WNH;
After Splitting to arrPrice:
[1588,8DNY],[1488,ACNY],[1288,7DPE],[1888,8HUC],[1488,8WNH]
2nd Splitting 2ndarrPrice:
[1588],[1488],[1288],[1888],[1488]
2nd Splitting 2ndarrCode:
[8DNY],[ACNY],[7DPE],[8HUC],[8WNH]
Sort Price in ascending order:
[1288],[1488],[1488],[1588],[1888]
Code will be sorted accordingly:
[7DPE],[ACNY],[8HUC],[8WNH]
I am stuck after the 1st splitting.
if (lblprices.Text != "")
{
arrprice = lblprices.Text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
Array.Sort(arrprice);
for (i = 0; i < arrprice.Length; i++)
{
arr2 = arrprice[i].Split(',');
SQL2 = "SELECT DISTINCT [TP].[TCode], ";
SQL2 += "[TP].[TName], ";
SQL2 += "[TP].[TName_CN], ";
SQL2 += "[TP].[TourType], ";
SQL2 += "[TP].[LastUpdateDate], ";
SQL2 += "[TP].[ValidityFrom], ";
SQL2 += "[TP].[ValidityTo], ";
SQL2 += "[CL].[CountryCode], ";
SQL2 += "[CL].[CityName] ";
SQL2 += "FROM [CL], [TP], [TourItinerary],[TourHotel] ";
SQL2 += "WHERE [TP].[Activation] = 1 ";
SQL2 += "AND [TP].[TCode] = '" + arr2[1] + "' ";
SQL2 += "ORDER BY [TP].[LastUpdateDate] DESC ";
objConnTour.Open();
SqlCommand command = new SqlCommand(SQL2, objConnTour);
SqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
html += "<tr><td class=\"border\">" + dataReader["TCode"] + "</td>";
html += "<td class=\"border\">" + dataReader["TName"] + "</td>";
html += "<td class=\"border\">" + dataReader["TName_CN"] + "</td>";
html += "<td class=\"border\">" + dataReader["TType"] + "</td>";
html += "<td class=\"border\">" + dataReader["LastUpdateDate"] + "</td>";
html += "<td class=\"border\">" + dataReader["ValidityFrom"] + "</td>";
html += "<td class=\"border\">" + dataReader["ValidityTo"] + "</td>";
html += "<td class=\"border\">" + dataReader["CountryCode"] + "</td>";
html += "<td class=\"border\">" + dataReader["CityName"] + "</td>";
html += "<td class=\"border\">from <span class=\"price-red\">S$<b>" + arr2[0] + "</b></span><td/></tr>";
}
dataReader.Close();
objConnTour.Close();
}
}
return html;
The code above will not be able to sort the price in ascending order. As you can see in my arr2 split, I need the Code and Price together as I will be retrieving data from database based on the Code.
——–Edit———
Ok so the problem is that if the prices are in the of 1000-1999, it gets sorted perfectly.
But if i have prices that is less than 1000, it will not be sorted in an ascending manner
Example are the screenshots of the results using the code provided by @jekcom
This is when i haven’t split the List and retrieve them raw from the database

And this is the Sorted one using @jekcom’s code

Notice how the Price are not sorted in Ascending manner.
The code you posted doesn’t work since you’re not sorting the array based on
Pricepart of the array. If you haveLinq:This should work, in sql terms this should give