local itemOrder = {
	["양손 도검류"] = 1,
	["양손 둔기류"] = 2,
	["양손 도끼류"] = 3,
	["장창류"] = 4,
	["지팡이류"] = 5,
	["한손 도검류"] = 6,
	["한손 도끼류"] = 7,
	["한손 둔기류"] = 8,
	["단검류"] = 9,
	["장착 무기류"] = 10,
	["총기류"] = 11,
	["활류"] = 12,
	["석궁류"] = 13,
	["투척 무기류"] = 14,
	["마법봉류"] = 15,
	["방패"] = 16,
	["천"] = 17,
	["가죽"] = 18,
	["사슬"] = 19,
	["판금"] = 20,
	["토템"] = 21,
	["우상"] = 22,
	["성서"] = 23,
	["보석"] = 24,
	["얼개"] = 25,
	["붉은색"] = 26,
	["노란색"] = 27,
	["푸른색"] = 28,
	["주황색 (노란+붉은)"] = 29,
	["녹색 (노란+푸른)"] = 30,
	["보라색 (붉은+푸른)"] = 31,
	["다색"] = 32,
	["가방"] = 33,
	["제조법"] = 34,
	["마법부여"] = 35,
	["대장기술"] = 36,
	["가죽세공"] = 37,
	["재봉술"] = 38,
	["연금술"] = 39,
	["기계 공학"] = 40,
	["보석세공"] = 41,
	["응급치료"] = 42,
	["소비용품"] = 43,
	["방어구"] = 44,
	["INVTYPE_2HWEAPON"] = 1,
	["INVTYPE_WEAPONMAINHAND"] = 2,
	["INVTYPE_WEAPON"] = 2,
	["INVTYPE_WEAPONOFFHAND"] = 2,
	["INVTYPE_SHIELD"] = 5,
	["INVTYPE_RANGEDRIGHT"] = 6,
	["INVTYPE_RANGED"] = 6,
	["INVTYPE_THROWN"] = 8,
	["INVTYPE_HOLDABLE"] = 9,
	["INVTYPE_HEAD"] = 10,
	["INVTYPE_SHOULDER"] = 11,
	["INVTYPE_CHEST"] = 12,
	["INVTYPE_ROBE"] = 12,
	["INVTYPE_HAND"] = 13,
	["INVTYPE_LEGS"] = 14,
	["INVTYPE_WAIST"] = 15,
	["INVTYPE_WRIST"] = 16,
	["INVTYPE_FEET"] = 17,
	["INVTYPE_CLOAK"] = 18,
	["INVTYPE_TRINKET"] = 19,
	["INVTYPE_NECK"] = 20,
	["INVTYPE_FINGER"] = 21,
	["INVTYPE_RELIC"] = 22,
}
local weaponOrder = {
	["INVTYPE_WEAPONMAINHAND"] = 1,
	["INVTYPE_WEAPON"] = 2,
	["INVTYPE_WEAPONOFFHAND"] = 3,
}
local tonumber = _G.tonumber
local tinsert = _G.table.insert
local sort = _G.sort
local GetItemInfo = _G.GetItemInfo

local pt, ut, ot, et, id
local name1, rarity1, level1, type1, subType1, equipLoc1, el1, name2, rarity2, level2, type2, subType2, equipLoc2, el2

function BlueItemInfo2:SortItemTable(items)
	pt, ut, ot, et = {}, {}, {}, {}
	for i = 1, #(items) do
		id = tonumber(items[i])
		if id > 0 then
			local name, _, _, _, _, _, subType, _, equipLoc = GetItemInfo(id)
			if name then
				if equipLoc and itemOrder[equipLoc] then
					tinsert(pt, id)
				else
					tinsert(ot, id)
				end
			else
				tinsert(ut, id)
			end
		else
			tinsert(et, id)
		end
	end
	sort(pt, function(a, b)
		name1, _, rarity1, level1, _, _, subType1, _, equipLoc1 = GetItemInfo(a)
		name2, _, rarity2, level2, _, _, subType2, _, equipLoc2 = GetItemInfo(b)
		el1, el2 = equipLoc1, equipLoc2
		equipLoc1, equipLoc2 = itemOrder[equipLoc1], itemOrder[equipLoc2]
		subType1, subType2 = itemOrder[subType1] or 99, itemOrder[subType2] or 99
		if equipLoc1 == equipLoc2 and subType1 == subType2 and rarity1 == rarity2 and level1 == level2 then
			if weaponOrder[el1] and weaponOrder[el2] and el1 ~= el2 then
				return weaponOrder[el1] < weaponOrder[el2]
			end
			return name1 < name2
		elseif equipLoc1 == equipLoc2 and subType1 == subType2 and rarity1 == rarity2 then
			return level1 > level2
		elseif equipLoc1 == equipLoc2 and subType1 == subType2 then
			return rarity1 > rarity2
		elseif equipLoc1 == equipLoc2 then
			return subType1 < subType2
		else
			return equipLoc1 < equipLoc2
		end
	end)
	sort(ot, function(a, b)
		name1, _, rarity1, _, _, type1, subType1 = GetItemInfo(a)
		name2, _, rarity2, _, _, type2, subType2 = GetItemInfo(b)
		subType1, subType2 = itemOrder[subType1] or 99, itemOrder[subType2] or 99
		if type1 == type2 and subType1 == subType2 and rarity1 == rarity2 then
			return name1 < name2
		elseif type1 == type2 and subType1 == subType2 then
			return rarity1 > rarity2
		elseif subType1 == subType2 then
			return type1 > type2
		else
			return subType1 > subType2
		end
	end)
	sort(et, function(a, b)
		return self:GetItemInfo(a) < self:GetItemInfo(b)
	end)
	sort(ut)
	for i = 1, #(ot) do
		tinsert(pt, ot[i])
	end
	for i = 1, #(et) do
		tinsert(pt, et[i])
	end
	for i = 1, #(ut) do
		tinsert(pt, ut[i])
	end
	return pt, #(ut)
end