0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その648

0
Posted at

概要

cscの作法、調べてみた。
Neo.IronLua、見つけたので、やってみた。
luaのarrayをc#から、取得する。

サンプルコード

using System;
using System.Collections.Generic;
using System.Linq;
using Neo.IronLua;

public static class Program {
	public static void SortLuaTable(LuaTable table) {
		int n = table.Values.Count;
		for (int i = 0; i < n - 1; i++)
		{
			for (int j = 1; j <= n - 1 - i; j++)
			{
				object val1 = table[j];
				object val2 = table[j + 1];
				if (CompareObjects(val1, val2) > 0)
				{
					table[j] = val2;
					table[j + 1] = val1;
				}
			}
		}
	}
	private static int CompareObjects(object x, object y) {
		if (x == null && y == null)
			return 0;
		if (x == null)
			return -1;
		if (y == null)
			return 1;
		if (IsNumeric(x) && IsNumeric(y))
		{
			return Convert.ToDouble(x).CompareTo(Convert.ToDouble(y));
		}
		return string.Compare(x.ToString(), y.ToString(), StringComparison.Ordinal);
	}
	private static bool IsNumeric(object value) {
		return value is int || value is double || value is float || value is long || value is short;
	}
	public static void Main() {
		using (Lua lua = new Lua())
		{
			LuaGlobal env = lua.CreateEnvironment();
			string luaCode = "myArray = { 52, 12, 89, 5, 23 }";
			env.DoChunk(luaCode, "script.lua");
			LuaTable luaTable = (LuaTable) env["myArray"];
			SortLuaTable(luaTable);
			Console.WriteLine("--- ソート結果 ---");
			for (int i = 1; i <= luaTable.Values.Count; i++)
			{
				Console.WriteLine("Index {0}: {1}", i, luaTable[i]);
			}
		}
	}
}


実行結果

>neo3
--- ソート結果 ---
Index 1: 5
Index 2: 12
Index 3: 23
Index 4: 52
Index 5: 89

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?