GeekyNuns GeekyNuns - 1 month ago 4x
ASP.NET (C#) Question

How to write sorting in more efficient way?

I have a project where I have to write an efficient code which will be working as fast as possible, but I have lack of knowledge do to it so...

So I have an project using entity framework and as well I have to use Web Service to get info about details from it.
First I make request to Web service and is responds with a long string, which i have to parse in a list of strings for further activities.

I parse this string like this:

string resultString;
char[] delimiterChars = { ',', ':', '"', '}', '{' };
List<string> words = resultString.Split(delimiterChars).ToList();

From here i have list with a lot of rows, which have information and a lot of junk rows, which look like this:

list of strings

I decided to clear this list from junk info, so as not to work with it in further methods and not to check this rows with ifs and so on:

for (int i = words.Count - 1; i >= 0; i--)
if (words[i] == "" || words[i] == "data" || words[i] == "array") words.RemoveAt(i);

After this I got clear list, but every decimal number like prices, sizes and so on got separated by
, so if I had price 21,55 in my list it now looks like 2 elements 21 and 55. I cant just delete
from separators, because string I get as a response from web service mainly separates info by putting

So I decided to glue decimal numbers back (before this block list elements looked like: 1)attrValue 2)21 3)55 and after like : 1)attrValue 2)21.55):

for (int i = 0; i < words.Count(); i++)
if (words[i] == "attrValue")
var seconPartInt = Int32.Parse(words[i + 2]);
words[i + 1] += "." + words[i + 2];
catch { }
if (words[i].Contains("\\/")) words[i].Replace("\\/", "/");

Every thing is ok, list is sorted, decimals are gathered, but speed is slowed down by 30%. After some tests with stopwatch and commenting blocks of code it became clear that this code above slows down the whole program too much...

To sum up:
I cant use that slow code and at the same time do not know how to make it work faster. May be the problem is that I convert string to int so as to check whether next element in the list is second part if my number.

How could I optimize my code?


The first thing you should do is use this version of Split to avoid getting empty entries (

List<string> words = resultString.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries)

Also, if you know that "data" and "array" are in the string and you never want them, replace them with blanks before you split the string.

resultString = resultString.Replace("data", String.Empty)
    .Replace("array", String.Empty);

What I don't understand is how the comma can be both a field delimiter and a meaningful character, and how you can possibly know the difference (i.e. whether 25,50 should be a single value or two values).