I was thinking if good way short strings whit character value. Like A = 0, B = 1...
I made some code, but best i get is wrong answer and i think i know where is problem but i dont know what is the problem.
So here is code:
void Start()
{
i = Inventory.instance;
List<Items> items = i.returnList("Food");
Debug.Log(items.Count); List<Items> sItems = GetListSetList(items);
foreach (Items item in sItems)
{
Debug.Log(item.name);
}
}
List items = i.returnList ("Food"); <-- This part is where i ask inventory return all "Food" taged items. Code is here:
public List<Items> returnList(string tag)
{
List<Items> classifiedItemSet = new List<Items>();
foreach (Items i in items)
{
if (i.tag == tag)
{
classifiedItemSet.Add(i);
}
}
return classifiedItemSet;
}
List sItems = GetListSetList (items); <-- This is part where i really try short list what i get from inventory. It looks like this :
private List<Items> GetListSetList(List<Items> itemS)
{
foreach (Items item in itemS)
{
GetAlphabetaValue(item);
}
List<Items> shorted = new List<Items>();
Items[] hold = new Items[itemS.Count];
foreach (Items item in itemS)
{
for (int x = 0; x < hold.Length; x++)
{
if (hold[x] == null)
{
hold[x] = item;
break;
}
else
{
bool PassIt = true;
for (int c_value = 0; c_value < item.Alphabet_value.Length; c_value++)
if (item.Alphabet_value[c_value] > hold[x].Alphabet_value[c_value])
PassIt = false;
if (PassIt)
{
for (int h_pos = hold.Length - 1; h_pos > x; h_pos--)
if (hold[h_pos] != null)
hold[h_pos] = hold[h_pos - 1];
hold[x] = item;
break; // If i use this break i get error ("NullReferenceException")
}
else continue;
}
}
}
for (int x = 0; x < hold.Length; x++)
shorted.Add(hold[x]);
return shorted;
}
Start of this void i give every character in every items name string some value. It is this part : GetAlphabetaValue (item); Oh and sorry naming it AlphaBeta :) Okey how i get this values is this :
private void GetAlphabetaValue(Items x)
{
x.Alphabet_value = new int[x.name.Length];
for (int c = 0; c < x.Alphabet_value.Length; c++)
{
string character = x.name.Substring(c, 1);
character.ToLower();
switch (character)
{
case "a":
x.Alphabet_value[c] = 0;
break;
case "b":
x.Alphabet_value[c] = 1;
break;
case "c":
x.Alphabet_value[c] = 2;
break;
case "d":
x.Alphabet_value[c] = 3;
break;
case "e":
x.Alphabet_value[c] = 4;
break;
case "f":
x.Alphabet_value[c] = 5;
break;
//To the end
}
}
}
I hope you understand what i try to talk :D Thank you :) And i try find some information in internet before i start doing this, but i dont find anything how really can short multiple string from array.
This part i think i got wrong but now i cant just see what wrong whit that :
for (int h_pos = hold.Length - 1; h_pos > x; h_pos--)
if (hold [h_pos] != null)
{
hold [h_pos] = hold [h_pos - 1];
hold [x] = item;
}
To start with do you really think it's a good idea to have a +20 switch-case
?
Characters are already numbered, characters a-z
in enligsh alphabet correspond to 97-122
chars in Unicode
.
This function:
void GetAlphabetaValue(Items x)
{
x.Alphabet_value = new int[x.name.Length];
for (int c = 0; c < x.Alphabet_value.Length; c++)
{
string character = x.name.Substring (c, 1);
character.ToLower ();
switch (character)
{
case "a":
x.Alphabet_value [c] = 0;
break;
///Etc... //Etc..... And end.
}
}
}
Becomes this:
void GetAlphabetaValue2(Items x)
{
var CharIntList = List<int>;
foreach (char ch in x.Name)
CharIntList.Alphabet_value.Add(ch - 97);
x.Alphabet_value = CharIntList.ToArray();
}
Much simpler. Also you code is messy, hard to understand and bad formatted. Probably you are new to C# so you should read about how you are supposed to write code, It's not like I do it well, but other pepole should be able to understand your code.
Then about you question, I think you mean to sort
not to short
(Completly different things). Also why is Items
in plural? it's a singular thing, then it should be Item
.
Well, I don't know about your problem, but you can replace your GetListSetList()
function to this:
private List<Items> GetListSetList(List<Items> items)
{
foreach (Items item in items)
{
GetAlphabetaValue(item);
Array.Sort(item.Alphabet_value);
}
return items;
}