按特定变量排序班级列表

时间:2017-10-04 12:14:35

标签: c# .net linq sorting

我有一个类列表,其中包含一个字符串。我想用来对列表进行排序的字符串如下所示:"A1534-11"

我想在信后用4个数字来命令它们。基本的OrderBy方法只是按字母排序。我怎么能这样做?

这就是我想要的:

"A1534-11"
"D1535-11"
"C1536-11"

2 个答案:

答案 0 :(得分:7)

使用LINQ和TakeWhile方法:

var result = lst.OrderBy(c => int.Parse(string.Join("",c.Name.Skip(1)
                             .TakeWhile(char.IsDigit)))).ToList();

我假设您的班级有一个名为Name的媒体资源。当然,您应该将其更改为您的财产的名称。

答案 1 :(得分:6)

您可以解析该子字符串并将结果整数用于LINQ方法import pygame, sys, os, math, time, pygame.surfarray ## SCREEN ## pygame.init() w,h = 800,600; cx,cy = w//2, h//2; os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.display.set_caption('Test') screen = pygame.display.set_mode((w,h),0) mapsurface = pygame.display.set_mode((w,h),1) ## IMPORT MAPS ## map_1 = pygame.image.load("assets/maps/map1.png") ## Creates a new 'screen' for map to display (under game screen / invisible) ## mapsurface.blit(map_1,(0,0)) class MapArray: map_array = pygame.surfarray.array3d(mapsurface) map_array_X = 0 map_array_Y = 0 map_array_output_colour_RED = [0] map_array_output_colour_GREEN = [0] map_array_output_colour_BLUE = [0] map_array_output_X = [0] map_array_output_Y = [0] DrawTiles = 0 PRINT = 0 def __init__(self,pos=(0,0)): self.pos = list(pos) def update(self,dt,key): colourRED_array = self.map_array[self.map_array_X,self.map_array_Y,0] colourGREEN_array = self.map_array[self.map_array_X,self.map_array_Y,1] colourBLUE_array = self.map_array[self.map_array_X,self.map_array_Y,2] ## MAP ARRAY SCAN - Gives X/Y coords and RGB value for each pixel in MAP ## if self.map_array_X <= 64 and colourBLUE_array < 255: self.map_array_X +=1 self.map_array_output_colour_RED.append(colourRED_array) self.map_array_output_colour_GREEN.append(colourGREEN_array) self.map_array_output_colour_BLUE.append(colourBLUE_array) self.map_array_output_X.append(self.map_array_X) self.map_array_output_Y.append(self.map_array_Y) if self.map_array_X == 64 and colourBLUE_array < 255: self.map_array_Y+=1 self.map_array_X = 0 if colourBLUE_array < 255: print (self.map_array_output_X, self.map_array_output_Y, self.map_array_output_colour_RED, self.map_array_output_colour_GREEN, self.map_array_output_colour_BLUE) print ("") elif self.PRINT == 0: print ("Map Scan Finished!"); print ("");print ("Format: [Xpos_List] [Ypos_List] [colourRED_array] [colourGREEN_array] [colourBLUE_array]"); self.PRINT = 1; self.DrawTiles = 1; if key[pygame.K_ESCAPE]: pygame.quit(); sys.exit() #Quits Game# clock = pygame.time.Clock() Map_Array = MapArray((0,0)) while True: dt = clock.tick(60) ## BACKGROUND ## screen.fill((100,100,100)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit();sys.exit() pygame.display.update() key = pygame.key.get_pressed() Map_Array.update(dt, key)

OrderBy