This question already has answers here:
Sort String array in custom order
(4 answers)
Fastest way to get list in custom order
(2 answers)
Closed 3 years ago.
How can I sort an array using a predefined sorted array?
I'm working with a web API that you can query for a list of information, which you can specify the things you need in the list. The data list gets returned separated by newlines.
The problem is that the API returns the information in a specific order, regardless of what order you specify yourself.
For example,
Query("second,third,first,fourth");
// returns string:
#"Info for first
Info for second
Info for third
Info for fourth"
I then have to parse it into a dictionary:
{ "first", "Info first" }, {"second", "Info second"}, etc
I could just base it off the parameter list I used, however unless you memorize the correct order for all data, it's a bit annoying.
So, how could I sort it using a predefined sorted list. Such as:
// All possible queries sorted correctly
{ "first", "second", "third", "fourth", "fifth", etc }
// My unsorted list
{ "third", "first", "fifth"}
// Would become:
{ "first", "third", "fifth"}
(These are placeholder values to make it more clear)
You can sort them using a method to compare these results to determine which one comes earlier according to your list
private bool IsBefore(string A, string B)
{
int iA, iB;
iA = Array.IndexOf(RefArray, A);
iB = Array.IndexOf(RefArray, B);
if (A < B)
return true;
return false;
}
Related
This question already has answers here:
Natural Sort Order in C#
(18 answers)
Closed 1 year ago.
I have a list similar to this:
List<Item> myList = new List<myObj>();
myList.Add(new Item({ val = A1, text="A1 - Alpha1"});
myList.Add(new Item({ val = A2, text="A2 - Alpha2"});
myList.Add(new Item({ val = A10, text="A10 - Alpha10"});
myList.Add(new Item({ val = A, text="A - Alpha"});
I'm looking to order this list alphabetically by their values so:
"A", "A1", "A2", "A10"
would be the desired order but I end up getting:
"A", "A1", "A10", "A2"
when using Linq's .OrderBy(x=>x.val) method.
I have tried some answers on Stack Overflow using custom ordering but these answers don't account for a mixture of values where it may or may not contain a numeric entry. Some of these orders are involving conversions to ints which won't satisfy all the values in my list.
How would it be possible to obtain the correct ordered result?
Please try below code
.OrderBy(x => Regex.Match(x.val, #"\d+").Value == "" ? 0 : Convert.ToInt32(Regex.Match(x.val, #"\d+").Value)).ThenBy(x => Regex.Match(x.val, #"[^A-Z]+").Value)
Thanks to #KlausGütter I was pointed in the right direction to solve this! I used Matthey Horsleys answer from this thread:
https://stackoverflow.com/a/11720793/4611941
as an extension method for IEnumerables and was able to simply call this method, passing in the parameter to sort by and it handled it perfectly!
i.e. after implementing the extension, I called
myList.OrderByAlphaNumeric(x=> x.val);
and it sorts correctly.
This question already has answers here:
How do I make my string compare not sensitive to (ignore) minor differences in white space?
(4 answers)
Closed 3 years ago.
I've tried several methods to remove duplicate elements from an array of strings, but none of them do what I want. Here are 2 strings:
CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NOSPACE//
CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NO SPACE//
I want just one of these to be retained as they are copied from array a to array b. It doesn't matter which one.
I have tried IEnumerable, HashSet, and Distinct. Each of them returns both strings. (An error of mine duplicated the second string. Sorry. To be clear, I want the compare to ignore whitespace.)
IEnumerable<string> b = a.AsQueryable().Distinct(StringComparer.InvariantCulture);
HashSet<string> set = new HashSet<string>(a);
string[] b = new string[set.Count];
set.CopyTo(b);
string[] b = a.Distinct().ToArray();
The first element isnt the same as the others, so distinct will not gonna work for this, you must replace the space char.
string[] a = { "CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NOSPACE//", "CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NO SPACE//", "CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NO SPACE//" };
string[] b = a.Select(p => p.Replace(" ", "")).Distinct().ToArray(); //Replace
output:
"CNTY/013121/261538/Y/Y/Y/Y/Y/Y/C/NOSPACE//",
This question already has answers here:
Sending array of objects to WCF
(3 answers)
Closed 5 years ago.
I have an array of objects
let arr = [{"1":"bar"},{"2":"bar"}]
which gets sent to a service through ajax inside data
the service will then get the array & do stuff.
[WebInvoke]
public void getStuff(params Model[] data)
{
// do stuff
}
what would my model need to look like to receive the data arr?
Update:
changed keys in object
You can do something like this to get the params:
var key = Request.Params[0];
then you can use the var "key" to fill a model
The elemenst in this .js array:
let arr = [{"foo":"bar"},{"foo":"bar"}]
Could be represented as this .cs class
class Model
{
public string foo;
}
Because for each object (the bit inside the {}), the default is to map the lhs to a property of the class with the same name (foo).
But, after your edit, if you want this:
let arr = [{"1":"bar"},{"2":"bar"}]
Then that cannot map to a class so easily, not leat because you can't have a field named '1' but also because it implies that there are many many different options for the lhs of the json.
In that case, consider using a Dictionary
This question already has answers here:
What is the .NET equivalent of PHP var_dump?
(5 answers)
Closed 6 years ago.
I need to dump the content of arrays or objects and I am interested to know if in C# we have something like PHP instruction var_dump.
The objective is to not build a loop to use every property or content of array or object and print with Console.WriteLine.
The closest thing would probably be string.Join:
Console.WriteLine(string.Join(", ", myEnumOfObjects));
It would not automatically include "every property or content of array or object" into the output, though - if you want that to happen, you need to override the ToString method of the object being printed:
class MyObject {
public string Name {get;set;}
public DateTime Dob {get;set;}
public override string ToString() {
return string.Format("{0} - {1}", Name, Dob);
}
}
I think there aren't direct equivalent of var_dump php function.
You must use reflection to write an equivalent function.
If you search in web, you can easily find code which do it.
For example : http://ruuddottech.blogspot.fr/2009/07/php-vardump-method-for-c.html
When you insert a break point you can easily view the contents of an array by hovering your mouse over it.
or any of these:
You are probably using Console.WriteLine for printing the array.
int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
Console.WriteLine(item.ToString());
}
If you don't want to have every item on a separate line use Console.Write:
int[] array = new int[] { 1, 2, 3 };
foreach(var item in array)
{
Console.Write(item.ToString());
}
or string.Join (in .NET Framework 4 or later):
int[] array = new int[] { 1, 2, 3 };
Console.WriteLine(string.Join(",", array));
from this question: How to print contents of array horizontally?
I know you want to avoid loop, but if its just for the sake of writing multiple lines of code, below is a one liner loop that could allow you to print data with single line for Objects extend ForEach Method
List<string> strings=new List<string>{"a","b","c"};//declare one
strings.ForEach(x => Console.WriteLine(x));//single line loop...for printing and is easier to write
Say I have
List<int> ages = new List<int>() { 8, 5, 3, 9, 2, 1, 7 };
List<int> marks = new List<int>() { 12, 17, 08, 15, 19, 02, 11 };
I can sort my marks by ages like this:
while (true)
{
bool swapped = false;
for (int i = 0; i < ages.Count - 1; i++)
if (ages[i] > ages[i + 1])
{
int tmp = ages[i];
ages[i] = ages[i + 1];
ages[i + 1] = tmp;
tmp = marks[i];
marks[i] = marks[i + 1];
marks[i + 1] = tmp;
swapped = true;
}
if (!swapped)
break;
}
Now I want to put this into a function that accepts any two lists. The first parameter will be the reference list, the numerical or comparable list. The second parameter will be the list containing the data.
For example:
public static void Sort<T>(List<T> RefList, List<T> DataList)
{
// sorting logic here...
}
There are a few problems:
First of all, T is almost certainly not the same type in RefList and DataList. RefList might be dates, integers, or doubles; whereas DataList is free to be absolutely anything. I need to be able to receive two, arbitrary generic types.
Secondly, I cannot seem to use the > operator with the T in this line:
if (ages[i] > ages[i + 1])
Perhaps my whole approach is wrong.
By the way, I have read responses to similar questions that suggest that the two lists should be combined into a single list of a compound data type. This isn't practical at all for my application. All I want to do is write a static function that somehow sorts one list based on the elements of another.
To sort one list the way you want you actually need to somehow keep references from items in first list to they weight/keys in the second list. No existing methods do that as you can't easily associate metadata with arbitrary values (i.e. if first list is list of int as in your case there is nothing to map to keys in second list). Your only reasonable option is to sort 2 lists at the same time and make association by index - again no existing classes to help.
It may be much easier to use solution that you reject. I.e. simply Zip and OrderBy, than recreate first list:
ages = ages
.Zip(marks, (a,m)=> new {age = a; mark = m;})
.OrderBy(v => v.mark)
.Select(v=>v.age)
.ToList();
Note (courtesy of phoog): if you need to do this type of sorting with Array there is Array.Sort that allows exactly this operatiion (see phoog's answer for details).
There's no framework method to do this with List<T>, but if you don't mind putting the data into two arrays, you can use one of the Array.Sort() overloads that takes two arrays as arguments. The first array is the keys, and the second is the values, so your code might look like this (leaving aside the step of getting arrays from the lists):
Array.Sort(ages, marks);
The specifics of getting the values into arrays and then back into lists would depend, among other things, on whether you need to end up with the same list sorted appropriately or whether it's okay to return a new list with the data in the desired order.
Use:
public static void Sort<TR, TD>(IList<TR> refList, IList<TD> dataList)
where TR : System.IComparable<TR>
where TD : System.IComparable<TD>
{
...
}
and then use:
refList[i].CompareTo(refList[i+1])
instead of the operators.
.Net numbers already implement IComparable, and you can use overloads that allow you to specify a different IComparable.
If I understand "I can sort my marks by ages like this:" properly,
I would like to suggest the below to eliminate much confusion.
struct Student{
int age;
int marks;
};
List<Student> students = {{8,12}, ...};
Now you can sort according to age and marks is accordingly sorted automatically.
If it is not possible, you need to fix the code as below.
First of all, T is almost certainly not the same type in RefList and DataList.
Then you need 2 parameters T1, T2. Just T implies the types are the same.
public static void Sort<RefType, DataType>(List<RefType> RefList, List<DataType> DataList)
{
You can also zip the two lists together as suggested by Mechanical Snail and explained in Looping through 2 Lists at once